
-- blatantly copied from http://lua-users.org/wiki/StringRecipes
function url_decode(str)
   str = str:gsub("+", " ")
   str = str:gsub("%%(%x%x)", function(h)
      return string.char(tonumber(h,16))
   end)
   str = str:gsub("\r\n", "\n")
   return str
end

function Image(elem, attr)
    local prefix = string.sub(elem.src, 1, 2)
    if prefix == "fa" then
        local fa_classes = url_decode(elem.src)
        io.stderr:write("font-awesome found: " .. fa_classes .. "\n");
        local content = ""
        for k, v in pairs(elem.caption) do
            if v.text ~= nil then
                -- io.stderr:write("content " .. v.text .. "\n")
                content = content .. " " .. v.text
            end
        end
        return pandoc.RawInline("html", "<i class='" .. fa_classes .. "'>" .. content .. "</i>")
    end
    if prefix == "fi" then
        local fi_classes = url_decode(elem.src)
        io.stderr:write("foundation-icon found: " .. fi_classes .. "\n");
        local content = ""
        for k, v in pairs(elem.caption) do
            if v.text ~= nil then
                content = content .. " " .. v.text
            end
        end
        return pandoc.RawInline("html", "<i class='" .. fi_classes .. "'>" .. content .. "</i>")
    end
    if prefix == "gg" then
        local gg_classes = url_decode(elem.src)
        io.stderr:write("cssgg found: " .. gg_classes .. "\n");
        local content = ""
        for k, v in pairs(elem.caption) do
            if v.text ~= nil then
                content = content .. " " .. v.text
            end
        end
        return pandoc.RawInline("html", "<i class='" .. gg_classes .. "'>" .. content .. "</i>")
    end
end
