1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
-- pandoc filters for www.btbytes.com
-- typical invocation:
-- pandoc -C -s --csl=../csl/jcsl.csl --bibliography=../bib/nbrefs.bib --lua-filter=../../pandoc/filters.lua tst.md -o tst.html

-- generated by ChatGPT and modified by me
-- function Str(elem)
--     local text = elem.text

--     -- Pattern for [[Article|/log/foo.html]]
--     local linked_pattern = "%[%[(.-)|(/.-)%]%]"
--     -- Pattern for [[Random Forests|random-forests]]
--     local simple_pattern = "%[%[(%w+)%]%]"
--     -- Pattern for [[Random Forests|random-forests]]
--     local title_wikilink_pattern = "%[%[(.- +)|(%w-)%]%]"

--     local replacement = text:gsub(linked_pattern, function(label, link)
--         return '<a href="' .. link .. '" class="wikilink fa-thin fa-location-dot">' .. label .. '</a>'
--     end)

--     replacement = replacement:gsub(simple_pattern, function(tag)
--         return '<a href="/' .. tag .. '.html" class="wikilink fa-thin fa-location-dot">' .. tag .. '</a>'
--     end)

--     replacement = replacement:gsub(title_wikilink_pattern, function(label, tag)
--         return '<a href="/' .. tag .. '.html" class="wikilink fa-thin fa-location-dot">' .. label .. '</a>'
--     end)

--     if replacement ~= text then
--         return pandoc.RawInline('html', replacement)
--     end
-- end
--     -- Pattern to match [[label|link]]
--     local label_link_pattern = "%[%[(.-)|(/.-)%]%]" --"%[%[(.-)|(/?.-)%]%]"
--	-- Pattern to match [[label|simple-link]]
--	local label_simple_link_pattern = "%[%[(.-)|([^%s]+)%]%]"
--
--	-- Replacement for the label|link pattern
--	text = text:gsub(label_link_pattern, function(label, link)
--		return '<a href="' .. link .. '" class="wikilink">' .. label .. "</a>"
--	end)
--
--	-- Replacement for the label|simple-link pattern
--	text = text:gsub(label_simple_link_pattern, function(label, link)
--		return '<a href="/' .. link .. '.html" class="wikilink">' .. label .. "</a>"
--	end)
--


function Str(elem)
    local text = elem.text

    -- Pattern to match [[link]] where link doesn't contain spaces
    local simple_link_pattern = "%[%[([^%s]+)%]%]"

    -- Pattern to match [[A Space Odyssey]] style link with spaces
    local spaced_link_pattern = "%[%[([%s+%S+])%]%]"

    -- Replacement for the spaced link pattern
    text = text:gsub(spaced_link_pattern, function(link)
        -- print("spaced_link_pattern, text=" .. text .. "link=" .. link .. " ------ ")
        return '<a href="/' .. link:gsub(" ", "-") .. '.html" class="wikilink spacedlink">' .. link .. "</a>"
    end)

    -- Replacement for the simple link pattern
    text = text:gsub(simple_link_pattern, function(link)
        -- print("simple_link_pattern, text=" .. text .. "link=" .. link .. " ------ ")
        return '<a href="/' .. link .. '.html" class="wikilink simplelink">' .. link .. "</a>"
    end)
    if elem.text:match("twitter:%a+") then
        local handle = elem.text:match("twitter:(%a+)")
        local link = pandoc.Link("@" .. handle, "https://twitter.com/" .. handle)
        local icon = pandoc.RawInline("html", '<i class="fa fa-twitter"></i>')
        return pandoc.Span { link, icon }
    end

    local blocklinkmatch = text:match("^([0-9a-fA-F]{6})$")
    if blocklinkmatch then
        return pandoc.Span({ pandoc.Str("#") }, { id = blocklinkmatch })
    else
        return pandoc.Str(text)
    end

    if text ~= elem.text then
        return pandoc.RawInline("html", text)
    else
        return elem
    end
end

-- generated by ChatGPT and modified by me
function Link(el)
    local patterns = {
        ["https://www.youtube.com/"] = '<i class="fa fa-youtube"></i>',
        ["https://twitter.com/"] = '<i class="fa fa-twitter"></i>',
        ["https://x.com/"] = '<i class="fa fa-twitter"></i>',
        ["https://github.com/"] = '<i class="fa fa-github"></i>',
        ["https://gist.github.com/"] = '<i class="fa fa-github"></i>',
    }

    for prefix, icon_html in pairs(patterns) do
        if el.target:sub(1, #prefix) == prefix then
            local icon = pandoc.RawInline("html", icon_html)
            table.insert(el.content, 1, icon)
            break
        end
    end

    local suffixes = {
        [".pdf"] = '<i class="fa far fa-file-pdf"></i>',
    }

    for suffix, icon_html in pairs(suffixes) do
        if el.target:sub(- #suffix) == suffix then
            local icon = pandoc.RawInline("html", icon_html)
            table.insert(el.content, 1, icon)
            return el
        end
    end

    return el
end