-
Notifications
You must be signed in to change notification settings - Fork 13
/
Toc.hs
92 lines (85 loc) · 5.07 KB
/
Toc.hs
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
{-# OPTIONS_GHC -fno-warn-tabs #-}
{-# LANGUAGE RecordWildCards, OverloadedStrings, ViewPatterns, NamedFieldPuns #-}
module Toc (writeTocFiles) where
import qualified Data.Text as Text
import qualified Data.Text.Lazy as LazyText
import qualified Data.Text.Lazy.Builder as TextBuilder
import Data.Time.Format (formatTime, defaultTimeLocale)
import Data.Time.Clock (getCurrentTime, UTCTime)
import Prelude hiding ((.), (++), writeFile)
import LaTeXBase (LaTeXUnit(..))
import Pages (Link(..), fileContent, applyPageStyle, PageStyle(..), outputDir, writePage)
import Render (secnum, linkToSection, simpleRender2, RenderContext(..), render, defaultRenderContext, Page(..))
import Util
import Document (Section(..), Draft(..), SectionKind(..), indexCatName, isDefinitionSection)
tocSection :: Draft -> Bool -> Section -> TextBuilder.Builder
tocSection _ _ Section{sectionKind=DefinitionSection _} = ""
tocSection draft expanded s@Section{..} =
xml "div" [("id", abbreviation)] $ header ++ mconcat (tocSection draft expanded . subsections)
where
header = h (min 4 $ 2 + length parents) $
secnum (if expanded then "#" ++ urlChars abbreviation else "") s ++ " "
++ render ( sectionName ++ [TeXRaw " "]
, (linkToSection (if expanded then SectionToSection else TocToSection) abbreviation){aClass="abbr_ref"})
defaultRenderContext{page=if expanded then ExpandedTocPage else TocPage, inSectionTitle=True, draft=draft}
++ "<div style='clear:right'></div>"
tocChapter :: Draft -> Bool -> Section -> TextBuilder.Builder
tocChapter draft expanded s@Section{abbreviation, sectionName, subsections, parents} =
xml "div" [("id", abbreviation)] $
h (min 4 $ 2 + length parents) header ++
xml "div" [("class", "tocChapter")] (mconcat (tocSection draft expanded . subsections))
where
href
| expanded = "SectionToSection/" ++ urlChars abbreviation
| otherwise = (if any (not . isDefinitionSection . sectionKind) subsections then "#" else "TocToSection/") ++ urlChars abbreviation
link = anchor{
aClass = "folded_abbr_ref",
aText = TextBuilder.fromText $ "[" ++ abbreviation ++ "]",
aHref = href}
header
| abbreviation == "bibliography" =
render anchor{aText = "Bibliography", aHref = href}
defaultRenderContext{inSectionTitle=True, draft=draft}
| otherwise =
secnum (if expanded then "#" ++ urlChars abbreviation else "") s ++ " " ++
render (sectionName ++ [TeXRaw " "], link) defaultRenderContext{inSectionTitle=True, draft=draft} ++
(if expanded then "" else simpleRender2 (linkToSection TocToSection abbreviation){aClass="unfolded_abbr_ref"})
tocHeader :: UTCTime -> Text -> Text
tocHeader date commitUrl =
"(Generated on " ++ Text.pack (formatTime defaultTimeLocale "%F" date)
++ " from the <a href='" ++ commitUrl ++ "'>LaTeX sources</a>"
++ " by <a href='https://github.com/Eelis/cxxdraft-htmlgen'>cxxdraft-htmlgen</a>."
++ " This is <em>not</em> an ISO publication.)"
++ "<br><br>"
++ "<b>Note: this is an early draft. It's known to be incomplet and incorrekt, and it has lots of"
++ " b<span style='position:relative;left:-1.2pt'>a</span><span style='position:relative;left:1pt'>d</span>"
++ " for<span style='position:relative;left:-3pt'>matti<span style='position:relative;bottom:0.15ex'>n</span>g.</span></b>"
writeTocFiles :: PageStyle -> Draft -> IO ()
writeTocFiles sfs draft@Draft{..} = do
date <- getCurrentTime
tocCss <- readFile "toc.css"
let
descMeta = "<meta name='description' content='Browser-friendly rendering of a recent draft of the C++ standard'>"
tocStyle = "<style>" ++ TextBuilder.fromString tocCss ++ "</style>"
writeFile (outputDir ++ "/index.html") $ applyPageStyle sfs $ LazyText.toStrict $ TextBuilder.toLazyText $
fileContent "" "Draft C++ Standard: Contents" (descMeta ++ tocStyle) $
"<h1 style='text-align:center; hyphens:none; margin: 1cm'>Working Draft<br>Programming Languages — C++</h1>" ++
xml "div" [("class", "tocHeader")] (TextBuilder.fromText $ tocHeader date commitUrl) ++
"<br><h1>Contents</h1>" ++
mconcat (tocChapter draft False . chapters) ++
mconcat (h 2
. (\cat -> simpleRender2 anchor{aHref="TocToSection/" ++ cat, aText=indexCatName cat})
. ["generalindex", "grammarindex", "headerindex", "libraryindex", "conceptindex", "impldefindex"])
fullTocCss <- readFile "fulltoc.css"
let
fullTocStyle = "<style>" ++ TextBuilder.fromString fullTocCss ++ "</style>"
pathHome = if sfs == InSubdir then "../" else ""
writePage "fulltoc" sfs $ applyPageStyle sfs $ LazyText.toStrict $ TextBuilder.toLazyText $
fileContent pathHome "Draft C++ Standard: Contents" (descMeta ++ fullTocStyle) $
"<h1 style='text-align:center; hyphens:none; margin: 1cm'>Working Draft<br>Programming Languages — C++</h1>" ++
xml "div" [("class", "tocHeader")] (TextBuilder.fromText $ tocHeader date commitUrl) ++
"<br><h1>Contents</h1>" ++
mconcat (tocChapter draft True . chapters) ++
mconcat (h 2
. (\cat -> simpleRender2 anchor{aHref="SectionToSection/" ++ cat, aText=indexCatName cat})
. ["generalindex", "grammarindex", "headerindex", "libraryindex", "conceptindex", "impldefindex"])