site: add table of contents to all wiki pages with 2+ headings
[ci skip]
This commit is contained in:
		
							parent
							
								
									3aad6696c9
								
							
						
					
					
						commit
						2364a53f49
					
				
							
								
								
									
										15
									
								
								Shake.hs
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								Shake.hs
									
									
									
									
									
								
							| @ -342,7 +342,7 @@ main = do | |||||||
|     phony "mdcombinedmanual" $ need [ mdcombinedmanual ] |     phony "mdcombinedmanual" $ need [ mdcombinedmanual ] | ||||||
|     mdcombinedmanual %> \out -> do |     mdcombinedmanual %> \out -> do | ||||||
|       need mdmanuals |       need mdmanuals | ||||||
|       liftIO $ writeFile mdcombinedmanual "\\$toc\\$" -- # Big Manual\n\n -- TOC style is better without main heading, |       liftIO $ writeFile mdcombinedmanual $ addToc "" | ||||||
|       forM_ mdmanuals $ \f -> do -- site/hledger.md, site/journal.md |       forM_ mdmanuals $ \f -> do -- site/hledger.md, site/journal.md | ||||||
|         cmd_ Shell ("printf '\\n\\n' >>") mdcombinedmanual |         cmd_ Shell ("printf '\\n\\n' >>") mdcombinedmanual | ||||||
|         cmd_ Shell pandoc f towebmd |         cmd_ Shell pandoc f towebmd | ||||||
| @ -406,7 +406,7 @@ main = do | |||||||
|     phony "oldmanuals" $ need oldhtmlmanuals |     phony "oldmanuals" $ need oldhtmlmanuals | ||||||
| 
 | 
 | ||||||
|     -- Render one website page (main or wiki) as html, saved in sites/_site/. |     -- Render one website page (main or wiki) as html, saved in sites/_site/. | ||||||
|     -- Wiki pages will have a heading prepended. |     -- Wiki pages will have a heading and TOC placeholder prepended. | ||||||
|     -- All pages will have github-style wiki links hyperlinked. |     -- All pages will have github-style wiki links hyperlinked. | ||||||
|     "site/_site//*.html" %> \out -> do |     "site/_site//*.html" %> \out -> do | ||||||
|         let filename = takeBaseName out |         let filename = takeBaseName out | ||||||
| @ -420,13 +420,13 @@ main = do | |||||||
|             template = "site/site.tmpl" |             template = "site/site.tmpl" | ||||||
|             siteRoot = if "site/_site/doc//*" ?== out then "../.." else "." |             siteRoot = if "site/_site/doc//*" ?== out then "../.." else "." | ||||||
|         need [source, template] |         need [source, template] | ||||||
|         -- read markdown source, link any wikilinks, pipe it to pandoc, write html out |         -- read markdown source, link any wikilinks, maybe add a heading and TOC, pipe it to pandoc, write html out | ||||||
|         Stdin . wikiLink . (if iswikipage then addHeading pagename else id) <$> (readFile' source) >>= |         Stdin . wikiLink . (if iswikipage then addHeading pagename . addToc else id) <$> (readFile' source) >>= | ||||||
|           (cmd Shell pandoc "-" fromsrcmd "-t html" |           (cmd Shell pandoc "-" fromsrcmd "-t html" | ||||||
|                            "--template" template |                            "--template" template | ||||||
|                            ("--metadata=siteRoot:" ++ siteRoot) |                            ("--metadata=siteRoot:" ++ siteRoot) | ||||||
|                            ("--metadata=\"title:" ++ pagename ++ "\"") |                            ("--metadata=\"title:" ++ pagename ++ "\"") | ||||||
|                            "--lua-filter=tools/pandoc-site.lua" |                            "--lua-filter=tools/pandoc-toc.lua" | ||||||
|                            "-o" out ) |                            "-o" out ) | ||||||
| 
 | 
 | ||||||
|     -- HLEDGER PACKAGES/EXECUTABLES |     -- HLEDGER PACKAGES/EXECUTABLES | ||||||
| @ -707,6 +707,11 @@ type Markdown = String | |||||||
| addHeading :: String -> Markdown -> Markdown | addHeading :: String -> Markdown -> Markdown | ||||||
| addHeading h = (("# "++h++"\n\n")++) | addHeading h = (("# "++h++"\n\n")++) | ||||||
| 
 | 
 | ||||||
|  | -- | Prepend a table of contents placeholder. | ||||||
|  | addToc :: Markdown -> Markdown | ||||||
|  | addToc = ((tocMarker++"\n\n")++) | ||||||
|  |   where tocMarker = "$TOC$" | ||||||
|  | 
 | ||||||
| -- | Convert Github-style wikilinks to hledger website links. | -- | Convert Github-style wikilinks to hledger website links. | ||||||
| wikiLink :: Markdown -> Markdown | wikiLink :: Markdown -> Markdown | ||||||
| wikiLink = | wikiLink = | ||||||
|  | |||||||
| @ -5,14 +5,6 @@ function Header(h) | |||||||
|   return h |   return h | ||||||
| end | end | ||||||
| 
 | 
 | ||||||
| function isTocBlock(blk) |  | ||||||
|   if not (blk.t               == "Para")  then return false end |  | ||||||
|   if not  blk.content[1]                  then return false end |  | ||||||
|   if not (blk.content[1].t    == "Str")   then return false end |  | ||||||
|   if not (blk.content[1].text == "$toc$") then return false end |  | ||||||
|   return true |  | ||||||
| end |  | ||||||
| 
 |  | ||||||
| function markupLink(hAttr, headerText) | function markupLink(hAttr, headerText) | ||||||
|   local headerId         = hAttr.identifier |   local headerId         = hAttr.identifier | ||||||
|   local headerProperties = hAttr.attributes |   local headerProperties = hAttr.attributes | ||||||
| @ -43,13 +35,25 @@ function createTable(elems) | |||||||
|   return {navBegin, contentsP, markupElements(elems), navEnd} |   return {navBegin, contentsP, markupElements(elems), navEnd} | ||||||
| end | end | ||||||
| 
 | 
 | ||||||
|  | function isTocMarker(blk) | ||||||
|  |   local tocMarker = "$TOC$" | ||||||
|  |   if not (blk.t               == "Para")  then return false end | ||||||
|  |   if not  blk.content[1]                  then return false end | ||||||
|  |   if not (blk.content[1].t    == "Str")   then return false end | ||||||
|  |   return blk.content[1].text == tocMarker | ||||||
|  | end | ||||||
|  | 
 | ||||||
| function Pandoc(doc) | function Pandoc(doc) | ||||||
|   newBlocks = {} |   newBlocks = {} | ||||||
|   tocBlocks = createTable(pandoc.utils.hierarchicalize(headers)) |   tocBlocks = createTable(pandoc.utils.hierarchicalize(headers)) | ||||||
|   for _,blk in pairs(doc.blocks) do |   for _,blk in pairs(doc.blocks) do | ||||||
|     if isTocBlock(blk) then |     -- replace a TOC placeholder with the table of contents, | ||||||
|       for _,tocBlk in pairs(tocBlocks) do |     -- or nothing if there's less than two headings | ||||||
|         table.insert(newBlocks, tocBlk) |     if isTocMarker(blk) then | ||||||
|  |       if #headers > 1 then | ||||||
|  |         for _,tocBlk in pairs(tocBlocks) do | ||||||
|  |           table.insert(newBlocks, tocBlk) | ||||||
|  |         end | ||||||
|       end |       end | ||||||
|     else |     else | ||||||
|       table.insert(newBlocks, blk) |       table.insert(newBlocks, blk) | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user