Repository navigation
Expand file tree
/
Copy pathlinks-to-html.lua
More file actions
52 lines (51 loc) · 1.86 KB
/
Copy pathlinks-to-html.lua
File metadata and controls
52 lines (51 loc) · 1.86 KB
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
-- links-to-html.lua rewrites links to the site's own Markdown pages so the HTML
-- version links to the HTML version: "user_manual.md#keys" becomes
-- "user_manual.html#keys". The Markdown source keeps .md links, because the
-- Markdown is what a BonBon browser and cmarkbrowser read.
--
-- Only a relative link (no scheme, no leading "//" or "/") whose path ends in
-- .md or .markdown is changed; an address such as https://example.org/a.md
-- points at somebody else's page and is left alone.
function Link(el)
local target = el.target
if target:match("^[%a][%w+.-]*:") or target:match("^/") or target:match("^#") then
return nil
end
local path, rest = target:match("^([^#?]*)(.*)$")
local base = path:match("^(.*)%.md$") or path:match("^(.*)%.markdown$")
if base == nil then
return nil
end
if base == "README" then
base = "index"
end
el.target = base .. ".html" .. rest
return el
end
-- Pandoc gives each page the title the Makefile passes (the file name), which
-- says nothing in a browser tab or a bookmark. This sets the page's title from
-- what the page is: a manual page is "name(section)", the home page is marked
-- (the template shows the project's name alone), about.md is "About", and any
-- other page is its first "# " heading, else its file name.
function Pandoc(doc)
local file = (PANDOC_STATE.input_files[1] or ""):match("([^/]*)$")
local title
local name, section = file:match("^(.+)%.(%d)%.md$")
if name then
title = name .. "(" .. section .. ")"
elseif file == "README.md" then
doc.meta.home = true
return doc
elseif file == "about.md" then
title = "About"
else
for _, block in ipairs(doc.blocks) do
if block.t == "Header" and block.level == 1 then
title = pandoc.utils.stringify(block)
break
end
end
end
doc.meta.pagetitle = title or file:gsub("%.md$", "")
return doc
end