forked from Eelis/cxxdraft-htmlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSectionPages.hs
More file actions
125 lines (113 loc) · 4.75 KB
/
SectionPages.hs
File metadata and controls
125 lines (113 loc) · 4.75 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
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
122
123
124
125
{-# LANGUAGE OverloadedStrings, RecordWildCards, TupleSections, ViewPatterns #-}
module SectionPages (writeSectionFiles, writeFullFile, writeFiguresFile, writeTablesFile, writeIndexFiles) where
import Prelude hiding ((++), (.), writeFile)
import System.Directory (createDirectoryIfMissing)
import System.IO (hFlush, stdout)
import Control.Monad (forM_)
import qualified Data.Map as Map
import qualified Data.Text as Text
import Render
import Load14882
import Util
renderParagraph :: Text -> Paragraph -> Text
renderParagraph idPrefix Paragraph{..} =
(case paraNumber of
Just (render -> i) ->
xml "div" [("class", "para"), ("id", idPrefix ++ i)] .
(xml "div" [("class", "marginalizedparent")]
(render (anchor{
aClass = "marginalized",
aHref = "#" ++ idPrefix ++ i,
aText = i
})) ++)
_ -> id)
$ (if paraInItemdescr then xml "div" [("class", "itemdescr")] else id) (render paraElems)
type SectionAbbr = LaTeX
renderSection :: Maybe SectionAbbr -> Maybe SectionAbbr -> Bool -> Section -> (Text, Bool)
renderSection page specific parasEmitted s@Section{..}
| full = (, True) $
xml "div" [("id", secOnPage)] $ header ++
mconcat (map
(renderParagraph (if parasEmitted then secOnPage ++ "-" else ""))
paragraphs) ++
mconcat (fst . renderSection page Nothing True . subsections)
| not anysubcontent = ("", False)
| otherwise =
( header ++
mconcat (fst . renderSection page specific False . subsections)
, anysubcontent )
where
secOnPage
| Just (render -> x) <- page, Just sub <- Text.stripPrefix (x ++ ".") secname = sub
| otherwise = secname
secname = render abbreviation
full = specific == Nothing || specific == Just abbreviation
header = sectionHeader (min 4 $ 1 + length parents) s
(if specific == Nothing then "#" ++ secOnPage else "")
abbr
abbr
| specific == Just abbreviation && not (null parents)
= anchor
| Just sp <- specific, sp /= abbreviation, not (null parents)
= anchor{aHref = "SectionToSection/" ++ url abbreviation ++ "#" ++ url sp}
| otherwise = linkToSection
(if null parents then SectionToToc else SectionToSection)
abbreviation
anysubcontent =
or $ map (snd . renderSection page specific True)
$ subsections
writeSectionFile :: FilePath -> SectionFileStyle -> Text -> Text -> IO ()
writeSectionFile n sfs title body = do
file <- case sfs of
Bare -> return n
WithExtension -> return $ n ++ ".html"
InSubdir -> do
createDirectoryIfMissing True (outputDir ++ n)
return $ n ++ "/index.html"
writeFile (outputDir ++ file) $ applySectionFileStyle sfs $
fileContent (if sfs == InSubdir then "../" else "") title body
sectionHeader :: Int -> Section -> Text -> Anchor -> Text
sectionHeader hLevel s@Section{..} secnumHref abbr_ref = h hLevel $
secnum secnumHref s ++ " " ++
render sectionName ++ " " ++
render abbr_ref{aClass = "abbr_ref", aText = "[" ++ render abbreviation ++ "]"}
writeFiguresFile :: SectionFileStyle -> [Figure] -> IO ()
writeFiguresFile sfs figures = writeSectionFile "fig" sfs "14882: Figures" $
"<h1>List of Figures <a href='SectionToToc/fig' class='abbr_ref'>[fig]</a></h1>"
++ mconcat (r . figures)
where
r :: Figure -> Text
r f@Figure{figureSection=s@Section{..}, ..} =
"<hr>" ++
sectionHeader 4 s "" anchor{
aHref = "SectionToSection/" ++ url abbreviation
++ "#" ++ replace ":" "-" (url figureAbbr) }
++ renderFig True f
writeTablesFile :: SectionFileStyle -> [Table] -> IO ()
writeTablesFile sfs tables = writeSectionFile "tab" sfs "14882: Tables" $
"<h1>List of Tables <a href='SectionToToc/tab' class='abbr_ref'>[tab]</a></h1>"
++ mconcat (r . tables)
where
r :: Table -> Text
r t@Table{tableSection=s@Section{..}, ..} =
"<hr>" ++
sectionHeader 4 s "" anchor{
aHref = "SectionToSection/" ++ url abbreviation
++ "#" ++ replace ":" "-" (url $ head tableAbbrs) }
++ renderTab True t
writeFullFile :: SectionFileStyle -> [Section] -> IO ()
writeFullFile sfs chapters = writeSectionFile "full" sfs "14882" $
mconcat $ applySectionFileStyle sfs . fst . renderSection Nothing Nothing True . chapters
writeSectionFiles :: SectionFileStyle -> [Section] -> IO ()
writeSectionFiles sfs chapters = do
putStr " sections..";
let allAbbrs = concatMap abbreviations chapters
forM_ allAbbrs $ \abbreviation -> do
putStr "."; hFlush stdout
writeSectionFile (Text.unpack $ abbrAsPath abbreviation) sfs ("[" ++ render abbreviation ++ "]") $
(mconcat $ fst . renderSection (Just abbreviation) (Just abbreviation) False . chapters)
putStrLn $ " " ++ show (length allAbbrs)
writeIndexFiles :: SectionFileStyle -> Index -> IO ()
writeIndexFiles sfs index = forM_ (Map.toList index) $ \(Text.unpack -> cat, i) -> do
putStrLn $ " " ++ cat
writeSectionFile cat sfs ("14882: " ++ indexCatName cat) $ h 1 (indexCatName cat) ++ render i