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 ] | ||||
|     mdcombinedmanual %> \out -> do | ||||
|       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 | ||||
|         cmd_ Shell ("printf '\\n\\n' >>") mdcombinedmanual | ||||
|         cmd_ Shell pandoc f towebmd | ||||
| @ -406,7 +406,7 @@ main = do | ||||
|     phony "oldmanuals" $ need oldhtmlmanuals | ||||
| 
 | ||||
|     -- 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. | ||||
|     "site/_site//*.html" %> \out -> do | ||||
|         let filename = takeBaseName out | ||||
| @ -420,13 +420,13 @@ main = do | ||||
|             template = "site/site.tmpl" | ||||
|             siteRoot = if "site/_site/doc//*" ?== out then "../.." else "." | ||||
|         need [source, template] | ||||
|         -- read markdown source, link any wikilinks, pipe it to pandoc, write html out | ||||
|         Stdin . wikiLink . (if iswikipage then addHeading pagename else id) <$> (readFile' source) >>= | ||||
|         -- 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 . addToc else id) <$> (readFile' source) >>= | ||||
|           (cmd Shell pandoc "-" fromsrcmd "-t html" | ||||
|                            "--template" template | ||||
|                            ("--metadata=siteRoot:" ++ siteRoot) | ||||
|                            ("--metadata=\"title:" ++ pagename ++ "\"") | ||||
|                            "--lua-filter=tools/pandoc-site.lua" | ||||
|                            "--lua-filter=tools/pandoc-toc.lua" | ||||
|                            "-o" out ) | ||||
| 
 | ||||
|     -- HLEDGER PACKAGES/EXECUTABLES | ||||
| @ -707,6 +707,11 @@ type Markdown = String | ||||
| addHeading :: String -> Markdown -> Markdown | ||||
| 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. | ||||
| wikiLink :: Markdown -> Markdown | ||||
| wikiLink = | ||||
|  | ||||
| @ -5,14 +5,6 @@ function Header(h) | ||||
|   return h | ||||
| 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) | ||||
|   local headerId         = hAttr.identifier | ||||
|   local headerProperties = hAttr.attributes | ||||
| @ -43,14 +35,26 @@ function createTable(elems) | ||||
|   return {navBegin, contentsP, markupElements(elems), navEnd} | ||||
| 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) | ||||
|   newBlocks = {} | ||||
|   tocBlocks = createTable(pandoc.utils.hierarchicalize(headers)) | ||||
|   for _,blk in pairs(doc.blocks) do | ||||
|     if isTocBlock(blk) then | ||||
|     -- replace a TOC placeholder with the table of contents, | ||||
|     -- or nothing if there's less than two headings | ||||
|     if isTocMarker(blk) then | ||||
|       if #headers > 1 then | ||||
|         for _,tocBlk in pairs(tocBlocks) do | ||||
|           table.insert(newBlocks, tocBlk) | ||||
|         end | ||||
|       end | ||||
|     else | ||||
|       table.insert(newBlocks, blk) | ||||
|     end | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user