-
Notifications
You must be signed in to change notification settings - Fork 13
/
Render.hs
1293 lines (1181 loc) · 57.2 KB
/
Render.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# OPTIONS_GHC -fno-warn-tabs #-}
{-# LANGUAGE
OverloadedStrings,
RecordWildCards,
TupleSections,
ViewPatterns,
NamedFieldPuns,
LambdaCase,
TypeSynonymInstances,
FlexibleInstances #-}
module Render (
Render(render), concatRender, renderTab, renderFig, renderIndex, simpleRender, simpleRender2, squareAbbr,
linkToSection, secnum, Page(..), parentLink,
defaultRenderContext, isSectionPage,
RenderContext(..), renderLatexParas
) where
import Load14882 (parseIndex) -- todo: bad
import Document (
CellSpan(..), Cell(..), RowSepKind(..), Row(..), Element(..), Draft(..), Footnote(..),
TeXPara(..), Sentence(..), Abbreviation, sectionByAbbr, footnotes, ColumnSpec(..),
Section(..), Chapter(..), Table(..), Figure(..), Sections(..), figures, formulas, tables, Item(..),
IndexComponent(..), IndexTree, IndexNode(..), IndexKind(..), IndexEntry(..), Formula(..),
IndexPath, indexKeyContent, tableByAbbr, figureByAbbr, formulaByAbbr, Paragraph(..), Note(..), Example(..),
chapterOfSection)
import LaTeXBase (LaTeX, LaTeXUnit(..), ArgKind(..), MathType(..), lookForCommand, concatRaws,
renderLaTeX, trim, isMath, isCodeblock, texStripPrefix, texSpan, mapTeX)
import qualified Data.IntMap as IntMap
import Data.Text (isPrefixOf)
import qualified Data.Text.Lazy.Builder as TextBuilder
import Debug.Trace (trace)
import qualified Data.Text as Text
import qualified Data.Text.Lazy as LazyText
import qualified Text.HTML.TagSoup as Soup
import Data.Char (isAlpha, isSpace, isAlphaNum, toLower, isUpper, ord, isDigit, toUpper)
import Control.Arrow (second)
import qualified Prelude ()
import qualified MathJax
import Prelude hiding (take, (.), (++), writeFile)
import Data.List (find, nub, intersperse, (\\), sortOn, dropWhileEnd)
import qualified Data.Map as Map
import Data.Maybe (isJust, fromJust)
import Pages (Link(..))
import Sentences (linkifyFullStop)
import Util ((.), (++), replace, Text, xml, spanTag, anchor, Anchor(..), greekAlphabet,
urlChars, intercalateBuilders, replaceXmlChars, spanJust, h, partitionBy, mapHead)
import CxxParser (parseCppDirective, parseLiteral, parseComment)
kill, literal :: [String]
kill = words $
"clearpage renewcommand newcommand enlargethispage noindent indent vfill pagebreak setlength " ++
"caption capsep continuedcaption bottomline hline rowsep hspace endlist cline " ++
"hfill nocorr small endhead kill footnotesize rmfamily microtypesetup nobreak nolinebreak " ++
"topline FlushAndPrintGrammar left right protect = ! @ - xspace obeyspaces"
literal = ["#", "{", "}", "~", "%", ""]
simpleMacros :: [(String, Text)]
simpleMacros =
[ ("," , "<span style='white-space:nowrap'> </span>")
-- thin, non-breaking, non-stretching space
, ("\"" , "\"")
, ("`" , "`")
, ("prime" , "'")
, ("caret" , "^")
, ("copyright" , "©")
, ("textregistered" , "®")
, ("Cpp" , "C++")
, ("sum" , "∑")
, ("ell" , "ℓ")
, ("shr" , ">>")
, ("cv" , "cv")
, ("shl" , "<<")
, ("br" , "<br/>")
, ("linebreak" , "<br/>")
, ("sim" , "~")
, ("quad" , "  ")
, ("qquad" , "  ")
, ("indent" , " ")
, ("unun" , "__")
, ("^" , "^")
, ("ldots" , "…")
, ("vdots" , "⋮")
, ("dotsc" , "…")
, ("times" , "×")
, ("&" , "&")
, ("$" , "$")
, ("backslash" , "\\")
, ("textbackslash" , "\\")
, ("colcol" , "::")
, ("tilde" , "~")
, ("textasciitilde" , "~")
, ("hspace" , " ")
, ("space" , " ")
, ("equiv" , "≡")
, ("le" , " ≤ ")
, ("leq" , " ≤ ")
, ("ge" , " ≥ ")
, ("geq" , " ≥ ")
, ("neq" , " ≠ ")
, ("land" , " ∧ ")
, ("lor" , " ∨ ")
, ("cdot" , "·")
, ("cdots" , "⋯")
, ("to" , "→")
, ("rightarrow" , "→")
, ("mapsto" , "↦")
, ("sqrt" , "√")
, ("lfloor" , "⌊")
, ("rfloor" , "⌋")
, ("lceil" , "⌈")
, ("rceil" , "⌉")
, (";" , " ")
, ("min" , "<span class=\"mathrm\">min</span>")
, ("max" , "<span class=\"mathrm\">max</span>")
, ("bmod" , "<span class=\"mathrm\">mod</span>")
, ("exp" , "<span class=\"mathrm\">exp</span>")
, ("ln" , "<span class=\"mathrm\">ln</span>")
, ("log" , "<span class=\"mathrm\">log</span>")
, ("opt" , "<sub><small>opt</small></sub>")
, ("rightshift" , "<span class=\"mathsf\">rshift</span>")
, ("textlangle" , "⟨")
, ("textrangle" , "⟩")
, ("textmu" , "μ")
, ("tablerefname" , "Table")
, ("figurerefname" , "Figure")
, ("newline" , "<br>")
, (">" , "	")
, ("bnfindent" , "   ")
, ("\n" , "\n")
]
++ [(n, Text.pack [c]) | (n, c) <- greekAlphabet]
zwsp :: Text
zwsp = "​" -- U+200B ZERO WIDTH SPACE
makeSpan, makeDiv :: [String]
makeSpan = words "center mbox mathsf emph textsc phantom term mathtt textnormal textrm descr textsl textit mathit indented"
makeDiv = words "definition cvqual emph exitnote footnote mathit paras ttfamily TableBase table tabular longtable"
indexPathString :: IndexPath -> Text
indexPathString =
replace " " "_" . -- HTML forbids space.
Text.intercalate "," .
map (indexKeyContent . indexKey)
indexShortName :: Text -> Maybe IndexKind -> Text
indexShortName "grammarindex" (Just DefinitionIndexEntry) = "nt"
indexShortName "grammarindex" Nothing = "ntref"
indexShortName "conceptindex" (Just DefinitionIndexEntry) = "concept"
indexShortName "conceptindex" Nothing = "conceptref"
indexShortName "headerindex" (Just DefinitionIndexEntry) = "header"
indexShortName "headerindex" Nothing = "headerref"
indexShortName "generalindex" (Just DefinitionIndexEntry) = "def"
indexShortName "generalindex" _ = ""
indexShortName "libraryindex" _ = "lib"
indexShortName "impldefindex" _ = ""
indexShortName "bibliography" _ = "bib"
indexShortName cat _ = error $ "indexShortName: unrecognized category: " ++ Text.unpack cat
indexPathId :: Text -> Maybe IndexKind -> IndexPath -> Text
indexPathId category kind =
(indexShortName category kind ++) .
(":" ++) .
replace " " "%20" .
replace "'" "'" .
replace "&" "&" .
indexPathString
indexPathId2 :: RenderContext -> Int -> Text -> IndexPath -> Maybe IndexKind -> Text
indexPathId2 ctx entryNr cat path kind = indexPathId cat kind path ++ indexOccurrenceSuffix ctx entryNr
indexPathId3 :: RenderContext -> LaTeX -> Text
indexPathId3 ctx indices = indexPathId2 ctx inum icat ipath ikind
where (icat, ipath, inum, ikind) : _ = indexPaths indices
indexPathHref :: Text -> Maybe IndexKind -> IndexPath -> Text
indexPathHref cat kind = (("#" ++ indexShortName cat kind ++ ":") ++) . urlChars . replace "&" "&" . indexPathString
asId :: LaTeX -> Text
asId = mconcat . map f
where
f :: LaTeXUnit -> Text
f (TeXRaw t) = replace "\n" "_" $ replace " " "_" t
f (TeXComm "tcode" _ [(_, x)]) = asId x
f (TeXComm "noncxxtcode" _ [(_, x)]) = asId x
f (TeXComm "texttt" _ [(_, x)]) = asId x
f (TeXComm "textit" _ [(_, x)]) = asId x
f (TeXComm "mathsf" _ [(_, x)]) = asId x
f (TeXComm "xspace" _ []) = "_"
f (TeXBraces x) = asId x
f (TeXMath Dollar x) = asId x
f (TeXComm "texorpdfstring" _ [_, (_, x)]) = asId x
f x = error $ "asId: unexpected: " ++ show x
instance Render Anchor where
render Anchor{..} _ =
xml "a" ([("class", aClass) | aClass /= ""] ++
[("href" , aHref ) | aHref /= ""] ++
[("id" , aId ) | aId /= ""] ++
[("title", aTitle) | aTitle /= ""] ++
[("style", aStyle) | aStyle /= ""]) aText
class Render a where render :: a -> RenderContext -> TextBuilder.Builder
concatRender :: Render a => [a] -> RenderContext -> TextBuilder.Builder
concatRender x c = mconcat $ map (\y -> render y c) x
instance Render Char where render c _ = TextBuilder.singleton c
instance (Render a, Render b) => Render (a, b) where
render (x, y) = render x ++ render y
renderCodeblock :: String -> [(ArgKind, LaTeX)] -> LaTeX -> RenderContext -> TextBuilder.Builder
renderCodeblock env args code ctx =
(case (env, args) of
("codeblocktu", [(FixArg, title)]) -> (("<p>" ++ render title ctx ++ ":") ++)
("indexedcodeblock", [(FixArg, indices)]) ->
let
link = anchor
{ aClass = "itemDeclLink"
, aHref = "#" ++ urlChars (indexPathId3 ctx indices)
, aText = "🔗" }
in renderIndexed ctx "span" indices .
(xml "div" [("class", "marginalizedparent")] (render link ctx) ++)
_ -> id) $
xml "span" [("class", "codeblock")] (
highlightLines ctx{rawTilde=True, rawHyphens=True, rawSpace=True, inCodeBlock=True} $
concatRaws $ expandTcode (dropInitialNewline code))
where
dropInitialNewline :: LaTeX -> LaTeX
dropInitialNewline (TeXRaw (Text.uncons -> Just ('\n', rest)) : more) = TeXRaw rest : more
dropInitialNewline x = x
expandTcode :: LaTeX -> LaTeX
expandTcode [] = []
expandTcode (TeXComm "tcode" _ [(FixArg, x)] : y) = expandTcode (x ++ y)
expandTcode (x : y) = x : expandTcode y
renderOutputblock :: LaTeX -> RenderContext -> TextBuilder.Builder
renderOutputblock code ctx = xml "pre" [("class", "outputblock")] $
render code ctx{rawTilde=True, rawHyphens=True, rawSpace=True}
sameIdNamespace :: Maybe IndexKind -> Maybe IndexKind -> Bool
sameIdNamespace Nothing (Just IndexOpen) = True
sameIdNamespace (Just IndexOpen) Nothing = True
sameIdNamespace x y = x == y
isFullPage :: Page -> Bool
isFullPage FullPage = True
isFullPage _ = False
abbrIsOnPage :: Abbreviation -> Page -> Bool
abbrIsOnPage _ FullPage = True
abbrIsOnPage abbr TablesPage = "tab:" `isPrefixOf` abbr
abbrIsOnPage abbr FiguresPage = "fig:" `isPrefixOf` abbr
abbrIsOnPage abbr (FigurePage Figure{..}) = abbr == figureAbbr
abbrIsOnPage abbr (TablePage Table{..}) = abbr == tableAbbr
abbrIsOnPage abbr (SectionPage sec)
| "fig:" `isPrefixOf` abbr = abbr `elem` (figureAbbr . snd . figures sec)
| "eq:" `isPrefixOf` abbr = abbr `elem` (formulaAbbr . snd . formulas sec)
| "tab:" `isPrefixOf` abbr = abbr `elem` (tableAbbr . snd . tables sec)
| otherwise = abbr `elem` (abbreviation . sections sec)
abbrIsOnPage _ _ = False
pageIndexEntries :: RenderContext -> IntMap.IntMap IndexEntry
pageIndexEntries c
| SectionPage s <- page c = secIndexEntries s
| otherwise = indexEntryMap (draft c)
indexOccurrenceSuffix :: RenderContext -> Int -> Text
-- Returns the _ that distinguishes expr#def:object_expression from
-- expr#def:object_expression_ ([expr] has two definitions of 'object expression',
-- one for E1.E2 and one for E1.*E2.)
indexOccurrenceSuffix c indexNum = underscores
where
Just theEntry = IntMap.lookup indexNum (pageIndexEntries c)
ies
| SectionPage s <- page c = secIndexEntriesByPath s
| otherwise = indexEntriesByPath (draft c)
underscores = Text.pack
[ '_' | (i, e) <- fromJust (Map.lookup (indexPath theEntry) ies)
, indexCategory e == indexCategory theEntry
, sameIdNamespace (indexEntryKind e) (indexEntryKind theEntry)
, i < indexNum ]
instance Render LaTeX where
render (TeXComm "textbackslash" _ [] : y)
| (TeXRaw s : rest) <- y = \sec -> "\\" ++ render (TeXRaw $ if rawSpace sec then s else unspace s) sec ++ render rest sec
where
unspace s
| Just (c, cc) <- Text.uncons s, isSpace c = cc
| otherwise = s
render (TeXComm "itshape" _ [] : x) = ("<i>" ++) . (++ "</i>") . render x
render (x : y) = render x ++ render y
render [] = return ""
keywords :: [Text]
keywords = map Text.pack $ words $
"char8_t char16_t char32_t namespace struct void operator friend template typedef long short class double public extern " ++
"using char new union unsigned sizeof alignas typename virtual this return const_cast delete noexcept static_cast " ++
"reinterpret_cast mutable bool private protected inline constexpr consteval final volatile default explicit enum export asm " ++
"typeid dynamic_cast throw if else for do while goto auto concept requires decltype try catch static_assert wchar_t " ++
"case switch alignof break continue signed audit axiom override const register thread_local int float static module import " ++
"co_return co_await co_yield constinit"
-- todo: read the real keyword table instead
highlightLines :: RenderContext -> LaTeX -> TextBuilder.Builder
highlightLines ctx x
| (spaces, x') <- texSpan (== ' ') x, spaces /= "" = TextBuilder.fromText spaces ++ highlightLines ctx x'
| Just (directive, x') <- parseCppDirective x = spanTag "preprocessordirective" (render directive ctx) ++ highlight ctx x'
| TeXComm (Text.pack -> c) _ [(FixArg, y)] : more <- x, c `elem` ["terminal"] = spanTag c (highlightLines ctx y) ++ highlight ctx more
| i@(TeXComm cmd _ _) : more <- x, cmd `elem` ["index", "obeyspaces"] = render i ctx ++ highlightLines ctx more
| otherwise = highlight ctx x
highlightUnit :: RenderContext -> LaTeXUnit -> TextBuilder.Builder
highlightUnit ctx x = case x of
TeXComm "rlap" _ [(FixArg, text)] ->
spanTag "rlap" (highlight ctx text)
TeXComm "indexedspan" _ [(FixArg, text), (FixArg, indices)] ->
renderIndexed ctx "span" indices (highlight ctx text)
TeXComm "terminal" _ [(FixArg, y)] ->
spanTag "terminal" (highlight ctx y)
TeXComm c _ []
| c `elem` ["%", "&", "caret", "~"] -> spanTag "operator" (render x ctx)
| c == "#" -> spanTag "preprocessordirective" (render x ctx)
| c `elem` ["{", "}"] -> spanTag "curlybracket" (render x ctx)
TeXBraces y -> highlight ctx y
_ -> render x ctx
highlight :: RenderContext -> LaTeX -> TextBuilder.Builder
highlight ctx x
| Just x' <- texStripPrefix "\n" x = "\n" ++ highlightLines ctx x'
| (TeXRaw "" : t) <- x = highlight ctx t
| Just (lit, x') <- parseLiteral x = spanTag "literal" (render lit ctx) ++ highlight ctx x'
| Just (comment, x') <- parseComment x = spanTag "comment" (render comment ctx{inComment=True, rawTilde=False}) ++ highlightLines ctx x'
| Just x' <- texStripPrefix "<new>" x = spanTag "operator""<" ++ "new" ++ spanTag "operator" ">" ++ highlight ctx x'
-- keywords
| (a, x') <- texSpan p x, a /= "" = (case () of
_ | a `elem` keywords -> spanTag "keyword"
_ | a `elem` ["defined", "__has_include", "__has_cpp_attribute", "_Pragma"] -> spanTag "preprocessordirective"
_ | a `elem` ["nullptr", "true", "false"] -> spanTag "literal"
_ | otherwise -> id) (render (TeXRaw a) ctx) ++ highlight ctx x'
where p c = isAlphaNum c || c == '_'
highlight ctx (TeXRaw x : more)
| Text.head x `elem` ("'\"" :: String) = render (TeXRaw $ Text.take 1 x) ctx ++ highlight ctx (TeXRaw (Text.tail x) : more)
| Text.head x `elem` ("()"::String) = spanTag "parenthesis" (render (TeXRaw $ Text.take 1 x) ctx) ++ highlight ctx (TeXRaw (Text.tail x) : more)
| Text.head x `elem` ("{}"::String) = spanTag "curlybracket" (render (TeXRaw $ Text.take 1 x) ctx) ++ highlight ctx (TeXRaw (Text.tail x) : more)
| Text.head x `elem` ("[]"::String) = spanTag "squarebracket" (render (TeXRaw $ Text.take 1 x) ctx) ++ highlight ctx (TeXRaw (Text.tail x) : more)
| Text.head x `elem` ("<>"::String) = spanTag "anglebracket" (render (TeXRaw $ Text.take 1 x) ctx) ++ highlight ctx (TeXRaw (Text.tail x) : more)
| Text.head x == '#' = spanTag "preprocessordirective" "#" ++ highlight ctx (TeXRaw (Text.tail x) : more)
| Text.take 2 x == "::"
= spanTag "operator" (render (TeXRaw "::") ctx) ++ highlight ctx (TeXRaw (Text.drop 2 x) : more)
| Text.head x `elem` ("*&^.-+/!=|:?%~#"::String)
= spanTag "operator" (render (TeXRaw (Text.take 1 x)) ctx) ++ highlight ctx (TeXRaw (Text.tail x) : more)
| (a, x') <- Text.span (\c -> not (isAlphaNum c || c `elem` ("#%_(){}[]<>.*:?'\"+=-/|&!^~\n" :: String))) x, a /= ""
= render (TeXRaw a) ctx ++ highlight ctx (TeXRaw x' : more)
| otherwise = error ("shit: " ++ show x)
highlight ctx (x : more) = highlightUnit ctx x ++ highlight ctx more
highlight _ [] = ""
indexPaths :: LaTeX -> [(Text, IndexPath, Int, Maybe IndexKind)]
indexPaths indices =
[ (cat, path, entryNr, kind)
| [ (FixArg, [TeXRaw (Text.unpack -> read -> entryNr)])
, (OptArg, [TeXRaw cat])
, (FixArg, (parseIndex -> (path, kind))) ] <- lookForCommand "index" indices]
renderIndexed :: RenderContext -> Text -> LaTeX -> TextBuilder.Builder -> TextBuilder.Builder
renderIndexed ctx thing indices body = foldl f body (indexPaths indices)
where f t (cat, path, entryNr, kind) = xml thing [("id", indexPathId2 ctx entryNr cat path kind)] t
commasAnd :: [TextBuilder.Builder] -> TextBuilder.Builder
commasAnd [] = undefined
commasAnd [x] = x
commasAnd [x, y] = x ++ " and " ++ y
commasAnd [x, y, z] = x ++ ", " ++ y ++ ", and " ++ z
commasAnd (x : y) = x ++ ", " ++ commasAnd y
abbrTitle :: Text -> Bool -> RenderContext -> Text
abbrTitle "bibliography" _ _ = "Bibliography"
abbrTitle abbr includeAbbr ctx
| "tab:" `isPrefixOf` abbr
, Just Table{..} <- tableByAbbr (draft ctx) abbr =
"Table " ++ Text.pack (show tableNumber) ++ ": " ++
LazyText.toStrict (TextBuilder.toLazyText $ render tableCaption ctx{noTags=True})
| Just sec@Section{..} <- sectionByAbbr (draft ctx) abbr =
LazyText.toStrict $ TextBuilder.toLazyText $
secnumText sec ++ " " ++
render sectionName ctx{noTags=True} ++
TextBuilder.fromText (if includeAbbr then " [" ++ abbr ++ "]" else "")
| otherwise = ""
renderBreak :: RenderContext -> TextBuilder.Builder
renderBreak ctx = if noTags ctx then "\n" else "<br>"
renderIndexLink :: String -> [(ArgKind, [LaTeXUnit])] -> RenderContext -> TextBuilder.Builder
renderIndexLink cmd [(FixArg, txt), (FixArg, [TeXRaw cat]), (FixArg, rawIndexPath), (FixArg, abbr_arg)] ctx
| not (noTags ctx)
, Just abbr <- mabbr = render anchor
{ aText = render txt ctx{inLink=True}
, aHref = (if abbrIsOnPage abbr (page ctx) then "" else linkToSectionHref SectionToSection abbr)
++ indexPathHref cat kind p
, aTitle = abbrTitle abbr True ctx
, aClass = if cmd == "hiddenindexlink" then "hidden_link" else ""
} ctx
| otherwise = render txt ctx
where
(p, kind) = parseIndex rawIndexPath
resolved :: Maybe Text
resolved = case Map.lookup p $ indexEntriesByPath (draft ctx) of
Just entries
| (hd:_) <- [ abbreviation
| (_, IndexEntry{indexEntrySection=abbreviation, indexEntryKind}) <- entries
, indexEntryKind == kind
, not ("gram." `isPrefixOf` abbreviation) ] -> Just hd
_ -> Nothing
traceIfBad
| resolved == Nothing, cat /= "grammarindex", cat /= "bibliography" =
trace $ "\nbad index link: " ++ show (cat, rawIndexPath)
++ "\nlookup result: " ++ show (Map.lookup p $ indexEntriesByPath (draft ctx))
| otherwise = id
mabbr = traceIfBad $ case abbr_arg of
[] -> resolved
[TeXRaw x] -> Just x
y -> error $ "bad indexlink arg: " ++ show y
renderIndexLink _ _ _ = error "bad indexlink"
instance Render LaTeXUnit where
render (TeXRaw x ) = \RenderContext{..} -> TextBuilder.fromText
$ (if rawHyphens then id else replace "--" "–" . replace "---" "—")
$ (if not inCodeBlock then replace "''" "”" else id)
$ (if rawTilde then id else replace "~" " ")
$ (if not insertBreaks then id else
replace "::" (zwsp ++ "::" ++ zwsp) .
replace "\1" "__" .
replace "_" (if noTags then "_­" else "_<span class='shy'></span>") .
replace "__" "\1")
$ (if replXmlChars then replaceXmlChars else id)
$ x
render (TeXComm "br" _ _ ) = renderBreak
render TeXLineBreak = renderBreak
render (TeXComm "break" _ [] ) = renderBreak
render (TeXBraces t ) = render t
render m@(TeXMath _ _ ) = renderMath [m]
render (TeXComm "commentellip" _ []) = const $ spanTag "comment" "/* ... */"
render (TeXComm "ensuremath" _ [(FixArg, x)]) = renderMath x
render (TeXComm "hyperref" _ [_, (FixArg, x)]) = render x
render (TeXComm "label" _ [(FixArg, [TeXRaw x])]) = render anchor{aId = x, aClass = "index"}
render (TeXComm "ref*" x y) = render (TeXComm "ref" x y)
render (TeXComm "ref" _ [(FixArg, concatRaws -> [TeXRaw abbr])]) = \ctx@RenderContext{..} ->
let
linkText :: TextBuilder.Builder
linkText
| "tab:" `isPrefixOf` abbr
, Just Table{..} <- tableByAbbr draft abbr = TextBuilder.fromString $ show tableNumber
| "fig:" `isPrefixOf` abbr
, Figure{..} <- figureByAbbr draft abbr = TextBuilder.fromString $ show figureNumber
| "eq:" `isPrefixOf` abbr
, f@Formula{..} <- formulaByAbbr draft abbr = TextBuilder.fromText $ fullFormulaNumber f
| otherwise = squareAbbr (not noTags) abbr
renderLabelRef sec =
simpleRender2 anchor{
aHref = abbrHref (abbreviation sec) ctx ++ "#" ++ abbr,
aText = squareAbbr (not noTags) (abbreviation sec),
aTitle = abbrTitle (abbreviation sec) False ctx }
renderSectionRef =
simpleRender2 anchor{
aHref = abbrHref abbr ctx,
aText = linkText,
aTitle = abbrTitle abbr False ctx }
in if noTags then linkText else
case Map.lookup abbr (labels draft) of
Just sec -> renderLabelRef sec
Nothing
| SectionPage pageSec <- page, abbreviation pageSec == abbr -> linkText
| otherwise -> renderSectionRef
render (TeXComm "iref" _ [(FixArg, [TeXRaw abbrs])]) = \ctx ->
let renderAbbr abbr = render (TeXComm "ref" "" [(FixArg, [TeXRaw abbr])]) ctx
in " (" ++ mconcat (intersperse ", " $ map (renderAbbr . Text.strip) $ Text.splitOn "," abbrs) ++ ")"
render (TeXComm "nopnumdiffref" _ [(FixArg, [TeXRaw (Text.splitOn "," -> abbrs)])]) = \ctx ->
let f abbr = simpleRender2 anchor{aHref = abbrHref abbr ctx, aText = squareAbbr True abbr}
in "<b>Affected " ++ (if length abbrs == 1 then "subclause" else "subclauses") ++ ":</b> "
++ commasAnd (map f abbrs)
render (TeXComm "weblink" _ [(FixArg, text), (FixArg, href)])
= render anchor
{ aText = simpleRender2 text
, aHref = simpleRender href}
render (TeXComm "url" _ [(FixArg, u)])
= render anchor
{ aText = simpleRender2 u
, aHref = simpleRender u }
render (TeXComm "link" _ [(FixArg, txt), (FixArg, [TeXRaw abbr])])
= \ctx -> if noTags ctx then render txt ctx else render anchor{
aHref = abbrHref abbr ctx,
aText = render txt ctx{inLink=True},
aTitle = abbrTitle abbr True ctx} ctx
render (TeXComm c _ l) | c `elem` ["indexlink", "hiddenindexlink"] = renderIndexLink c l
render (TeXComm "color" _ _) = const ""
render (TeXComm "textcolor" _ [_, (FixArg, x)]) = render x
render (TeXComm "textsmaller" _ [_, (FixArg, x)]) = render x
render (TeXComm "terminal" _ [(FixArg, x)]) = spanTag "terminal" . flip highlightLines x
render (TeXComm "texttt" _ [(FixArg, x)]) = \ctx ->
(if noTags ctx then id else spanTag "texttt") $ render x ctx{rawHyphens = True, insertBreaks = True}
render (TeXComm "literaltcode" _ [(FixArg, x)]) = spanTag "literal" . spanTag "texttt" . render x
render (TeXComm cmd _ [(FixArg, x)])
| cmd `elem` ["tcode"] = \ctx ->
if noTags ctx then render x ctx{rawHyphens=True, insertBreaks=True}
else spanTag (if inCodeBlock ctx then "tcode_in_codeblock" else "texttt") $
if not (inComment ctx) && not (inLink ctx) && not (inSectionTitle ctx)
then highlightLines ctx{rawHyphens=True, insertBreaks=True} x
else render x ctx{rawHyphens=True, insertBreaks=True}
render (TeXComm "noncxxtcode" _ [(FixArg, x)]) = \ctx ->
spanTag (if inCodeBlock ctx then "tcode_in_codeblock" else "texttt") $
render x ctx{rawHyphens=True, insertBreaks=True}
render (TeXComm "textbf" _ [(FixArg, x)]) = ("<b>" ++) . (++ "</b>") . render x
render (TeXComm "index" _
[ (FixArg, [TeXRaw (Text.unpack -> read -> entryNr)])
, (OptArg, [TeXRaw category])
, (FixArg, (parseIndex -> (p, kind)))
])
= \ctx -> if noTags ctx then "" else case kind of
Just IndexClose -> ""
Just (See _ _) -> ""
_ -> render anchor
{ aId = indexPathId2 ctx entryNr category p kind
, aClass = "index"} ctx
render (TeXComm "indexedspan" _ [(FixArg, text), (FixArg, indices)]) =
\ctx -> (if noTags ctx then id else renderIndexed ctx "span" indices) $ render text ctx
render (TeXEnv "indexeditemdecl" [(FixArg, indices)] t) = \ctx ->
let
link = anchor
{ aClass = "itemDeclLink"
, aHref = "#" ++ urlChars (indexPathId3 ctx indices)
, aText = "🔗" }
in
renderIndexed ctx "div" indices $
xml "div" [("class", "itemdecl")] $
xml "div" [("class", "marginalizedparent")] (render link ctx) ++
xml "code" [("class", "itemdeclcode")] (TextBuilder.fromText $ Text.dropWhile (== '\n') $ LazyText.toStrict $ TextBuilder.toLazyText $ highlightLines ctx{rawTilde=True, rawHyphens=True} t)
render (TeXComm "discretionary" _ _) = const (TextBuilder.fromText zwsp)
render (TeXComm "ifthenelse" _ [_, _, (FixArg, x)]) = render x
render (TeXComm "multicolumn" _ [(FixArg, [TeXRaw n]), _, (FixArg, content)]) = xml "td" [("colspan", n)] . render content
render (TeXComm "leftshift" _ [(FixArg, content)]) =
(spanTag "mathsf" "lshift" ++) . xml "sub" [("class", "math")] . render content
render (TeXComm "verb" _ [(FixArg, a)]) = \c -> xml "code" [] $ render a c{rawTilde=True, rawHyphens=True}
render (TeXComm "footnoteref" _ [(FixArg, [TeXRaw n])]) = \ctx -> flip render ctx $ anchor
{ aClass = "footnoteref"
, aText = TextBuilder.fromText n
, aId = "footnoteref-" ++ n
, aTitle = (!! 3) $ iterate (Text.replace " " " ")
$ Text.replace "\n" " "
$ Text.replace "'" "'"
$ LazyText.toStrict $ TextBuilder.toLazyText
$ mconcat $ map (flip render ctx{noTags = True})
$ footnoteContent $ snd
$ footnotes (draft ctx) !! ((read (Text.unpack n) :: Int) - 1)
, aHref =
(if isFullPage (page ctx) || isSectionPage (page ctx) then "" else "SectionToSection/" ++ paraUrl ctx)
++ "#footnote-" ++ n }
render (TeXComm "raisebox" _ args)
| (FixArg, concatRaws -> [TeXRaw d]) <- head args
, (FixArg, content) <- Prelude.last args =
let neg s
| Text.head s == '-' = Text.tail s
| otherwise = "-" ++ s
in xml "span" [("style", "position: relative; top: " ++ neg d)] . render content
render (TeXComm "parbox" _ [_, (FixArg, x)]) = render x
render (TeXComm "term" _ [(FixArg, x)]) =
\sec ->
let
i = "def:" ++ asId x
-- It's tempting to use 'term:' instead of 'def:' here, but if we do that,
-- URLs break when upstream promotes a \term to a \defn.
in render anchor
{ aText = "<i>" ++ render x sec ++ "</i>"
, aId = i
, aHref = "#" ++ urlChars i
, aClass = "hidden_link" } sec
render (TeXComm "texorpdfstring" _ [_, (FixArg, x)]) = render x
render (TeXComm " " _ []) = return " "
render (TeXComm "\n" _ []) = return "\n"
render (TeXComm "textit" _ [(FixArg, x)]) = \c -> (if noTags c then id else xml "i" []) $ render x c{rawTilde = False}
render (TeXComm "c" _ [(FixArg, [TeXRaw "t"])]) = return "ţ"
render (TeXComm s _ [])
| s == "caret" = return "^"
| s `elem` literal = return $ TextBuilder.fromString s
| Just x <-
lookup s simpleMacros = return $ TextBuilder.fromText x
| s `elem` kill = return ""
| otherwise = return $ spanTag (Text.pack s) ""
render (TeXComm "class" _ [(FixArg, [TeXRaw cls]), (FixArg, [TeXComm "href" _ [(FixArg, [TeXRaw href]), (FixArg, text)]])])
= \ctx -> render anchor{aHref=href, aText=render text ctx, aClass=cls} ctx
render (TeXComm "class" _ [(FixArg, [TeXRaw cls]), (FixArg, x)])
= \ctx -> spanTag cls $ render x ctx
render (TeXComm "href" _ [(FixArg, [TeXRaw href]), (FixArg, text)])
= \ctx -> render anchor{aHref=href, aText=render text ctx} ctx
render (TeXComm "ucode" _ [(FixArg, code)]) = spanTag "ucode" . render (TeXRaw "U+" : code)
render (TeXComm x _ s)
| x `elem` kill = return ""
| null s, Just y <-
lookup x simpleMacros = return $ TextBuilder.fromText y
| [(FixArg, z)] <- s, Just y <-
lookup x simpleMacros = (TextBuilder.fromText y ++) . render z
| otherwise = \ctx -> (if noTags ctx then id else spanTag (Text.pack x)) $ render (s >>= snd) ctx
render (TeXEnv "itemdecl" [(FixArg, [TeXRaw num])] t) = \c ->
let
i = case [(icat, ipath) | (icat, ipath, _inum, Just DefinitionIndexEntry) <- indexPaths t] of
[(icat, ipath)] -> indexPathId icat (Just DefinitionIndexEntry) ipath
_ -> mconcat (idPrefixes c) ++ "itemdecl:" ++ num
link = anchor{aClass="itemDeclLink", aHref="#" ++ urlChars i, aText="🔗"}
in
xml "div" [("class", "itemdecl"), ("id", i)] $
xml "div" [("class", "marginalizedparent")] (render link c) ++
xml "code" [("class", "itemdeclcode")] (TextBuilder.fromText $ Text.dropWhile (== '\n') $ LazyText.toStrict $ TextBuilder.toLazyText $ highlightLines c{rawTilde=True, rawHyphens=True} t)
render env@(TeXEnv e args t)
| e `elem` makeSpan = \ctx -> (if noTags ctx then id else spanTag (Text.pack e)) (render t ctx)
| e `elem` makeDiv = xml "div" [("class", Text.pack e)] . render t
| isMath env && hasComplexMath True [env] = renderComplexMath [env]
| isCodeblock env = renderCodeblock e args t
| e == "minipage", [e2@(TeXEnv _ _ cb)] <- trim t, isCodeblock e2 =
xml "div" [("class", "minipage")] . renderCodeblock "codeblock" [] cb
| e == "outputblock" = renderOutputblock t
| e == "itemdescr" = render t
| e == "thebibliography" = render t
| otherwise = error $ "render: unexpected " ++ show env
instance Render Int where render = return . TextBuilder.fromString . show
instance Render IndexComponent where
render IndexComponent{..} = render indexKey
instance Render IndexEntry where
render IndexEntry{indexEntryKind=Just (See also x), ..} = \ctx ->
"<i>" ++ (if also then "see also" else "see") ++ "</i> " ++
render (anchor
{ aHref = "#:" ++
(urlChars $
replace " " "_" $
replace ", " "," $
indexKeyContent x)
, aText = render x ctx}) ctx
render IndexEntry{indexEntryKind=Just IndexClose} = return ""
render IndexEntry{..} =
return $ simpleRender2 anchor
{ aHref = "SectionToSection/" ++ urlChars indexEntrySection
++ indexPathHref indexCategory indexEntryKind indexPath
, aText = (if indexEntryKind == Just DefinitionIndexEntry then xml "b" [] else id) $ squareAbbr True indexEntrySection }
indexDisplayOrder :: IndexComponent -> (([(Int, Int)], Int), ([(Int, Int)], Int))
indexDisplayOrder y = (f (indexSortKey y), f (indexKey y))
where
g :: Char -> (Int, Int)
g c
| isDigit c = (1, ord c)
| isAlpha c = (2, ord (toLower c))
| otherwise = (0, ord c)
he :: String -> ([(Int, Int)], Int)
he x = (map g x, if isUpper (head x) then 0 else 1)
f = he . Text.unpack . indexKeyContent
instance Render [(IndexComponent, IndexNode)] where
render tree ctx = go [] tree
where
IndexPage cat = page ctx
go :: IndexPath -> [(IndexComponent, IndexNode)] -> TextBuilder.Builder
go up x = mconcat $ f up . (sortOn (indexDisplayOrder . fst) x)
f :: IndexPath -> (IndexComponent, IndexNode) -> TextBuilder.Builder
f up (comp, IndexNode{..}) =
let
up' = up ++ [comp]
in
xml "div" [("id", indexPathId cat Nothing up')] $
xml "div" [("class", "indexitems")] $
TextBuilder.fromText (
Text.intercalate ", " (nub $ filter (/= "") $ map (LazyText.toStrict . TextBuilder.toLazyText) $ render comp ctx : flip render ctx . indexEntries)) ++
go up' (Map.toList indexSubnodes)
data IndexHeading = Symbols | Numbers | Letter Char
deriving (Eq, Ord)
instance Show IndexHeading where
show Symbols = "Symbols"
show Numbers = "Numbers"
show (Letter c) = [c]
indexHeading :: IndexComponent -> IndexHeading
indexHeading (indexSortKey -> indexKeyContent -> Text.head -> c)
| isDigit c = Numbers
| isAlpha c = Letter (toUpper c)
| otherwise = Symbols
indexSortKey :: IndexComponent -> LaTeX
indexSortKey IndexComponent{..}
| distinctIndexSortKey /= [] = distinctIndexSortKey
| otherwise = indexKey
renderIndex :: RenderContext -> IndexTree -> TextBuilder.Builder
renderIndex ctx tree
| name `elem` ["generalindex", "libraryindex"] = mconcat $ ["<hr>"] ++ linklines ++ ["<hr>"] ++ map sub p
| otherwise = render (Map.toList tree) ctx
where
IndexPage name = page ctx
p = partitionBy (indexHeading . fst) $ Map.toList tree
sub (n, ii) = h 2 (render anchor{aText=TextBuilder.fromText $ Text.pack (show n), aId=Text.pack (show n)} ctx) ++ render ii ctx
(symnum, rest) = splitAt 2 p
linklines = map (h 2 . mconcat . intersperse " " . map (li . fst)) [symnum, rest]
li n = render anchor{aText = TextBuilder.fromText $ Text.pack (show n), aHref = "#" ++ Text.pack (show n)} ctx
renderTab :: Bool -> Table -> Text -> Bool -> Bool -> RenderContext -> TextBuilder.Builder
renderTab stripTab Table{..} href boldCaption linkifyTableNum ctx =
xml "div" [("class", "numberedTable"), ("id", id_)] $ -- todo: multiple abbrs?
(if boldCaption then "<b>" else "") ++
"Table " ++ tableNumF (render tableNumber ctx) ++ " — " ++
render tableCaption ctx ++
" " ++
render anchor{aText="[" ++ TextBuilder.fromText tableAbbr ++ "]", aHref=href} ctx ++
(if boldCaption then "</b>" else "") ++
"<br>" ++
renderTable columnSpec tableBody ctx
where
tableNumF = if linkifyTableNum then linkify anchor{aHref = "#" ++ id_} ctx else id
id_ = (if stripTab then replace "tab:" "" else id) tableAbbr
linkify :: Anchor -> RenderContext -> TextBuilder.Builder -> TextBuilder.Builder
linkify a ctx txt = render a{aText=txt} ctx
renderFig :: Bool -> Figure -> Text -> Bool -> Bool -> RenderContext -> TextBuilder.Builder
renderFig stripFig Figure{..} href boldCaption linkifyFigureNum ctx =
xml "div" [("class", "figure"), ("id", id_)] $
TextBuilder.fromText figureSvg ++ "<br>" ++
(if boldCaption then "<b>" else "") ++
"Figure " ++ figureNumF (render figureNumber ctx) ++ " — " ++
render figureName ctx ++ "  " ++
render anchor{aText=squareAbbr False figureAbbr, aHref=href} ctx ++
(if boldCaption then "</b>" else "")
where
figureNumF = if linkifyFigureNum then linkify anchor{aHref="#" ++ id_} ctx else id
id_ = (if stripFig then replace "fig:" "" else id) figureAbbr
data RenderItem = RenderItem { listOrdered :: Bool, item :: Item }
spacedJoin :: TextBuilder.Builder -> TextBuilder.Builder -> TextBuilder.Builder
spacedJoin x y
| TextBuilder.toLazyText x == "" = y
| TextBuilder.toLazyText y == "" = x
| otherwise = x ++ " " ++ y
instance Render RenderItem where
render RenderItem{item=Item Nothing mlabel elems paras} ctx
= xml "li" attrs $ render elems ctx ++ renderLatexParas paras ctx
where
attrs
| Just [TeXRaw l] <- mlabel = [("id", l)]
| otherwise = []
render RenderItem{item=Item (Just nn) mlabel elems paras, ..} ctx
| listOrdered =
xml "tr" [("id", thisId)] $
(xml "td" [] (case mlabel of
Nothing -> render link ctx'
Just label -> render anchor{aHref = linkHref, aText=simpleRender2 label} ctx' ++ " ")) ++
(xml "td" [] content)
| otherwise =
xml "li" [("id", thisId)] $ case mlabel of
Nothing -> xml "div" [("class", "marginalizedparent"), ("style", "left:" ++ left)] (render link ctx') ++ content
Just label ->
render anchor{aHref = linkHref, aText=simpleRender2 label} ctx'
++ " " ++ content
where
content = spacedJoin (render elems ctx') (renderLatexParas paras ctx')
left
| listOrdered = "-4.5em"
| otherwise = simpleRender (-marginalizedParentLeft - ulPaddingLeft * (length nn - 1) - extraIndentation ctx) ++ "mm"
ulPaddingLeft = 9
marginalizedParentLeft = 18
thisId = mconcat (idPrefixes ctx) ++ Text.pack (Prelude.last nn)
ctx' = ctx{ idPrefixes = idPrefixes ctx ++ [Text.pack (Prelude.last nn) ++ "."] }
dottedNumber = Text.intercalate "." (Text.pack . nn)
linkText
| listOrdered =
let
s = Prelude.last nn
punct
| isAlpha (head s) = ")"
| otherwise = "."
in
Text.pack $ s ++ punct
| otherwise = "(" ++ dottedNumber ++ ")"
linkClass
| listOrdered = "enumerated_item_num"
| otherwise = "marginalized"
linkHref = "#" ++ thisId
link = anchor{aClass=linkClass, aHref=linkHref, aText=TextBuilder.fromText linkText}
paraUrl :: RenderContext -> Text
paraUrl RenderContext{..} = urlChars $ abbreviation $ case nearestEnclosing of
Left p -> paraSection p
Right s -> s
prependSentence :: Sentence -> TeXPara -> TeXPara
prependSentence s (TeXPara ss) = TeXPara (s : ss)
instance Render Footnote where
render (Footnote n content) ctx =
xml "div" [("class", "footnote"), ("id", i)] $
renderParas (mapHead (prependSentence footnoteNum) content)
where
footnoteNum = Sentence Nothing [HtmlElement (LazyText.toStrict $ TextBuilder.toLazyText $ render (link, backlink) ctx)]
ctx' = ctx{idPrefixes = [i ++ "."]}
backlink = anchor{aText = linkText, aHref = "#footnoteref-" ++ num, aClass = "footnoteBacklink"}
renderParas [] = ""
renderParas (p:pp) = xml "div" [("class", "texpara")] (render p ctx') ++ renderParas pp
num = Text.pack $ show n
i = "footnote-" ++ num
footnoteIsOnPage = isFullPage (page ctx) || isSectionPage (page ctx)
linkText = TextBuilder.fromText $ num ++ ")"
link = anchor
{ aText = linkText
, aClass = "footnotenum"
, aHref =
(if footnoteIsOnPage then "" else "SectionToSection/" ++ paraUrl ctx)
++ "#" ++ i }
noWrapSpace :: TextBuilder.Builder
noWrapSpace = " "
instance Render Note where
render Note{..} ctx = xml "div" [("id", i), ("class", "note")] (renderParas True noteContent)
where
prefix = "[<i>" ++ TextBuilder.fromText noteLabel ++ " " ++ render link ctx ++ "</i>: "
suffix = " —" ++ noWrapSpace ++ "<i>end note</i>]"
renderParas _ [] = ""
renderParas isFirst (p:pp) = xml "div" [("class", "texpara")] ((if isFirst then prefix else "") ++ render p ctx ++ (if null pp then suffix else "")) ++ renderParas False pp
i = mconcat (dropWhileEnd (isDigit . Text.head) (idPrefixes ctx)) ++ "note-" ++ noteNum
noteNum = Text.pack $ show noteNumber
link = anchor{aHref = "#" ++ i, aText = TextBuilder.fromText noteNum }
instance Render Example where
render Example{..} ctx
| noTags ctx =
"[Example: "
++ renderLatexParas exampleContent ctx
++ " — end example] "
| otherwise = xml "div" [("id", i), ("class", "example")] (renderParas True exampleContent)
where
prefix = "[<i>Example " ++ render link ctx ++ "</i>: "
suffix = " —" ++ noWrapSpace ++ "<i>end example</i>]"
renderParas _ [] = ""
renderParas isFirst (p:pp) = xml "div" [("class", "texpara")] ((if isFirst then prefix else "") ++ render p ctx ++ (if null pp then suffix else "")) ++ renderParas False pp
i = mconcat (dropWhileEnd (isDigit . Text.head) (idPrefixes ctx)) ++ "example-" ++ exNum
exNum = Text.pack $ show exampleNumber
link = anchor{aHref = "#" ++ i, aText = TextBuilder.fromText exNum }
instance Render Formula where
render f@Formula{..} ctx =
xml "div" [("class", "formula"), ("id", formulaAbbr)] $
doRenderComplexMath [TeXMath Square ([tag] ++ formulaContent)] ctx
where tag = TeXComm "tag" "" [(FixArg, [TeXRaw (fullFormulaNumber f)])]
fullFormulaNumber :: Formula -> Text
fullFormulaNumber Formula{..} = Text.pack $ show chapterNum ++ "." ++ show formulaNumber
where chapterNum = sectionNumber $ chapterOfSection formulaSection
nontermDef :: LaTeX -> Maybe Text
nontermDef t
| [n] <- [n | ("grammarindex", [IndexComponent{distinctIndexSortKey=[TeXRaw n]}], _inum, Just DefinitionIndexEntry) <- indexPaths t] = Just n
| otherwise = Nothing
instance Render Element where
render (HtmlElement html) = const $ TextBuilder.fromText html
render (LatexElement x) = render x
render (Codeblock x) = render x
render (Itemdescr x) = xml "div" [("class", "itemdescr")] . renderLatexParas x
render (NoteElement x) = render x
render (ExampleElement x) = render x
render (Bnf e t) = xml "div" ([("class", Text.pack e)] ++ idattr) . render t
where
idattr
| Just nt <- nontermDef t = [("id", "nt:" ++ nt)]
| otherwise = []
render (TableElement t) = \ctx ->
renderTab False t ("./SectionToSection/" ++ tableAbbr t) False True ctx{idPrefixes=[tableAbbr t++"-"]}
render (FigureElement f) = renderFig False f ("./SectionToSection/" ++ figureAbbr f) False True
render (FormulaElement f) = render f
render (Tabbing t) =
xml "pre" [] . TextBuilder.fromText . htmlTabs . LazyText.toStrict . TextBuilder.toLazyText . render (preprocessPre t) -- todo: this is horrible
render Enumerated{..} = xml t [("class", Text.pack enumCmd)] .
concatRender (RenderItem (enumCmd == "enumerate") . enumItems)
where
t = case enumCmd of
"enumerate" -> "table"
"itemize" -> "ul"
"description" -> "ul"
"thebibliography" -> "ul"
_ -> undefined
class HasComplexMath a where
hasComplexMath :: Bool -> a -> Bool
instance HasComplexMath LaTeXUnit where
hasComplexMath mathMode (TeXRaw x) = mathMode && Text.any (`elem` ("+-*/^_=' " :: String)) (Text.strip x)
hasComplexMath m (TeXComm c _ args)
| c `elem` words "frac sum binom int sqrt lfloor rfloor lceil rceil log mathscr mapsto cdot bmod" = True
| c `elem` words "tcode" = hasComplexMath False (map snd args)
| otherwise = hasComplexMath m (map snd args)
hasComplexMath _ (TeXMath _ x) = hasComplexMath True x
hasComplexMath m (TeXBraces x) = hasComplexMath m x
hasComplexMath m (TeXEnv e _ args)
| e `elem` ["array", "eqnarray"] = True
| otherwise = hasComplexMath m args
hasComplexMath _ TeXLineBreak = False
instance HasComplexMath a => HasComplexMath [a] where
hasComplexMath m = any (hasComplexMath m)
data Page
= SectionPage Section
| TablePage Table
| FigurePage Figure
| FullPage
| IndexPage Text {- category -}
| XrefDeltaPage
| FootnotesPage
| TablesPage
| FiguresPage
| TocPage
| ExpandedTocPage
isSectionPage :: Page -> Bool
isSectionPage (SectionPage _) = True
isSectionPage _ = False
data RenderContext = RenderContext
{ page :: Page
, draft :: Draft
, nearestEnclosing :: Either Paragraph Section
, rawHyphens :: Bool -- in real code envs /and/ in \texttt
, rawTilde :: Bool -- in real code envs but not in \texttt
, rawSpace :: Bool
, insertBreaks :: Bool
, inLink :: Bool -- so as not to linkify grammarterms that appear as part of a defined/linkified term/phrase
, inCodeBlock :: Bool -- in codeblocks, some commands like \tcode have a different meaning
, inComment :: Bool -- in comments, \tcode should not be highlighted
, inSectionTitle :: Bool -- in section titles, there should be no highlighting
, replXmlChars :: Bool -- replace < with <, etc
, noTags :: Bool -- means we're rendering the contents of e.g. a "title" attribute which cannot contain tags/elements
, extraIndentation :: Int -- in em
, idPrefixes :: [Text] }
defaultRenderContext :: RenderContext
defaultRenderContext = RenderContext
{ page = error "no page"
, draft = error "no draft"
, nearestEnclosing = error "no para/sec"
, rawHyphens = False
, rawTilde = False
, rawSpace = False
, insertBreaks = False
, inLink = False
, inCodeBlock = False
, inComment = False
, inSectionTitle = False
, replXmlChars = True
, noTags = False
, extraIndentation = 0
, idPrefixes = [] }
squareAbbr :: Bool -> Abbreviation -> TextBuilder.Builder
squareAbbr softHyphens =
("[" ++) . (++ "]") .
TextBuilder.fromText .
(if softHyphens then Text.replace "." ".<span class='shy'></span>" else id)
parentLink :: Section -> Abbreviation -> Text
parentLink parent child
| Just sub <- Text.stripPrefix (abbreviation parent ++ ".") child = sub
| otherwise = child
abbrHref :: Abbreviation -> RenderContext -> Text
abbrHref abbr RenderContext{..}
| SectionPage sec <- page, abbreviation sec == abbr = "#"
| abbrIsOnPage abbr page = "#" ++ case page of
SectionPage sec -> urlChars (parentLink sec abbr)
TablesPage | Just abbr' <- Text.stripPrefix "tab:" abbr -> urlChars abbr'
_ -> urlChars abbr
| "fig:" `isPrefixOf` abbr =
let Figure{figureSection=Section{..}, ..} = figureByAbbr draft abbr
in "SectionToSection/" ++ urlChars abbr ++ "#" ++ urlChars figureAbbr
| "eq:" `isPrefixOf` abbr =
let Formula{formulaSection=Section{..}, ..} = formulaByAbbr draft abbr
in "SectionToSection/" ++ urlChars abbr ++ "#" ++ urlChars formulaAbbr
| "tab:" `isPrefixOf` abbr =
case tableByAbbr draft abbr of
Just Table{tableSection=Section{..}, ..} -> "SectionToSection/" ++ urlChars abbreviation ++ "#" ++ urlChars tableAbbr
_ -> "#" ++ urlChars abbr
| otherwise = linkToSectionHref SectionToSection abbr
prepMath :: LaTeX -> String
prepMath = Text.unpack . renderLaTeX . (>>= cleanup)