-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathRawDocument.hs
560 lines (505 loc) · 22.3 KB
/
RawDocument.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
{-# LANGUAGE
OverloadedStrings,
RecordWildCards,
ViewPatterns,
LambdaCase,
TupleSections,
NamedFieldPuns,
FlexibleInstances,
FlexibleContexts,
RankNTypes,
MultiParamTypeClasses,
FunctionalDependencies,
UndecidableInstances,
RecursiveDo #-}
module RawDocument
( RawElement(..), RawTexPara(..), RawFootnote(..), RawParagraph(..), LinearSection(..), RawItem(..)
, loadMacros, parseFile, loadXrefDelta, doParse) where
import qualified LaTeXParser as Parser
import qualified Data.Text as Text
import Data.Text (Text, replace)
import Document (Row(..), SourceLocation(..), RowSepKind(..), SectionKind(..), Cell(..), CellSpan(..), XrefDelta, Abbreviation, ColumnSpec(..), TextAlignment(..))
import Data.Maybe (isJust, fromJust)
import LaTeXParser (Macros(..), Signature(..), nullCmd, storeCmd, storeEnv, Environment(..), Command(..), codeEnv, Token(..), normalCmd, ParseResult(..))
import Data.Text.IO (readFile)
import Text.Regex (mkRegex)
import qualified Data.Map as Map
import Data.Map (Map)
import Data.List (transpose, take, isPrefixOf)
import Util ((.), (++), mapHead, textStripInfix, textSubRegex, splitOn)
import Prelude hiding (take, (.), takeWhile, (++), lookup, readFile)
import System.IO.Unsafe (unsafePerformIO)
import System.Process (readProcess)
import Control.Arrow (first)
import Data.Char (isSpace, isDigit)
import LaTeXBase
data RawItem = RawItem
{ rawItemLabel :: LaTeX
, rawItemContent :: [RawTexPara] }
deriving (Eq, Show)
data RawElement
= RawLatexElement LaTeXUnit
| RawEnumerated String [RawItem]
| RawCodeblock LaTeXUnit
| RawExample [RawTexPara]
| RawNote Text [RawTexPara]
| RawItemdescr [RawTexPara]
| RawBnf String LaTeX
| RawTable
{ rawTableCaption :: LaTeX
, rawColumnSpec :: [ColumnSpec]
, rawTableAbbr :: Abbreviation
, rawTableBody :: [Row [RawTexPara]] }
| RawTabbing LaTeX
| RawFormula { rawFormulaAbbr :: Abbreviation, rawFormulaContent :: LaTeX }
| RawFigure { rawFigureName :: LaTeX, rawFigureAbbr :: Abbreviation, rawFigureSvg :: Text }
deriving (Eq, Show)
newtype RawTexPara = RawTexPara { rawTexParaElems :: [RawElement] }
deriving (Eq, Show)
newtype RawFootnote = RawFootnote [RawTexPara]
deriving Show
data RawParagraph = RawParagraph
{ paraNumbered :: Bool
, rawParaInItemdescr :: Bool
, rawParaElems :: [RawTexPara]
, rawParaSourceLoc :: Maybe SourceLocation }
deriving Show
data LinearSection = LinearSection
{ lsectionAbbreviation :: Abbreviation
, lsectionKind :: SectionKind
, lsectionName :: LaTeX
, lsectionParagraphs :: [RawParagraph]
, lsectionFootnotes :: [RawFootnote] }
deriving Show
instance AllUnits RawElement where
allUnits (RawLatexElement x) = allUnits x
allUnits (RawBnf _ x) = allUnits x
allUnits (RawTabbing x) = allUnits x
allUnits (RawNote _ x) = allUnits x
allUnits (RawExample x) = allUnits x
allUnits (RawCodeblock x) = allUnits x
allUnits (RawItemdescr x) = allUnits x
allUnits (RawEnumerated _ x) = allUnits x
allUnits (RawFormula _ x) = allUnits x
allUnits RawFigure{} = []
allUnits RawTable{..} = allUnits rawTableCaption ++ concatMap (allUnits . concat . map content) (map cells rawTableBody)
instance AllUnits RawTexPara where
allUnits = allUnits . rawTexParaElems
instance AllUnits RawItem where
allUnits RawItem{..} = allUnits rawItemLabel ++ allUnits rawItemContent
instance AllUnits LinearSection where
allUnits LinearSection{..} = allUnits lsectionName ++ allUnits lsectionParagraphs ++ allUnits lsectionFootnotes
instance AllUnits RawParagraph where
allUnits RawParagraph{..} = allUnits rawParaElems
instance AllUnits RawFootnote where
allUnits (RawFootnote x) = allUnits x
bnfEnvs :: [String]
bnfEnvs = ["bnf", "ncbnf", "bnfkeywordtab", "simplebnf", "ncsimplebnf", "ncrebnf"]
isBnf :: LaTeXUnit -> Bool
isBnf (TeXEnv s _ _)
| s `elem` bnfEnvs = True
isBnf _ = False
isTable, isTabbing, isFigure :: LaTeXUnit -> Bool
isTable x = isTeXEnv "floattablebasex" x || isTeXEnv "htmlTable" x
isTabbing = isTeXEnv "tabbing"
isFigure = isTeXEnv "importgraphic"
isEnumerate :: LaTeXUnit -> Maybe String
isEnumerate (TeXEnv s _ _)
| s `elem` ["enumerate", "itemize", "description", "thebibliography"] = Just s
isEnumerate _ = Nothing
isParaEnd :: LaTeXUnit -> Bool
isParaEnd (TeXEnv "itemdecl" _ _) = True
isParaEnd (TeXEnv "indexeditemdecl" _ _) = True
isParaEnd (TeXEnv "itemdescr" _ _) = True
isParaEnd (TeXComm "pnum" _ _) = True
isParaEnd x = isParasEnd x
isParasEnd :: LaTeXUnit -> Bool
isParasEnd (TeXComm "definition" _ _) = True
isParasEnd (TeXComm "rSec" _ _) = True
isParasEnd (TeXComm "infannex" _ _) = True
isParasEnd (TeXComm "normannex" _ _) = True
isParasEnd _ = False
isJunk :: LaTeXUnit -> Bool
isJunk (TeXRaw x) = all isSpace (Text.unpack x)
isJunk (TeXComm "index" _ _) = True
isJunk (TeXComm "setlength" _ _) = True
isJunk _ = False
isItem :: LaTeXUnit -> Maybe LaTeX
isItem (TeXComm "item" _ []) = Just []
isItem (TeXComm "item" _ [(_, label)]) = Just label
isItem (TeXComm "bibitem" _ [(_, [TeXRaw label])]) = Just [TeXRaw $ "bib:" ++ label]
isItem _ = Nothing
parseItems :: LaTeX -> [RawItem]
parseItems [] = []
parseItems (x : rest)
| isJunk x = mapHead (mapItemContent (mapHead addJunk)) (parseItems rest)
| Just label <- isItem x, (item, rest') <- break (isJust . isItem) rest =
RawItem label (parsePara item) : parseItems rest'
where
mapItemContent f (RawItem l c) = RawItem l (f c)
addJunk :: RawTexPara -> RawTexPara
addJunk (RawTexPara z) = RawTexPara (dropWhile isOnlySpace $ RawLatexElement x : z)
parseItems _ = error "need items or nothing"
doParse :: Macros -> Text -> (LaTeX, Macros)
doParse m t = (x, y)
where
(x, y, []) = Parser.parseString ctx (Text.unpack t)
ctx = initialContext{Parser.macros=m}
nullCmds :: [(Int, String)]
nullCmds =
[ (0, "clearpage kill rmfamily hfill vfill nocorr small larger noindent itcorrwidth itletterwidth global")
, (1, "enlargethispage lstset newsavebox vspace")
, (2, "glossary settowidth addtolength")
, (3, "definecolor")
]
storeCmds :: [(Int, String)]
storeCmds =
[ (0, "today def makeatletter bottomline makeatother Sec bmod mod long prime " ++
"chapter section paragraph subparagraph fi otextup linebreak newpage log " ++
"textup edef x BnfIndent par leq " ++
"leftmargini BnfInc BnfRest protect caret sum " ++
"xspace onelineskip textlangle textrangle tilde raggedright = " ++
"space copyright textregistered textbackslash hsize br Gamma " ++
"frenchspacing list leftmargin listparindent itemindent itshape relax " ++
"nonfrenchspacing endlist upshape ttfamily baselineskip nobreak " ++
"endfirsthead quad qquad cdot cdots dotsc bnfindentinc footnotemark ldots capsep max min " ++
"continuedcaption hline endhead footnotesize le times dotsb rightarrow to equiv " ++
"lfloor rfloor pi geq neq ge lceil rceil ell alpha bigl bigr mu lambda beta " ++
"tabularnewline exp sigma big delta rho Pi nu infty displaystyle lim sin cos " ++
"phi int theta zeta FlushAndPrintGrammar break backslash centering " ++
"normalbaselineskip land lor mapsto normalfont textmu tablerefname figurerefname newline " ++
"obeyspaces bnfindent vdots tabcolsep columnbreak emergencystretch commentellip " ++
"gamma widowpenalties sffamily parskip left right `")
, (1, "hspace footnote textit textrm textnormal texttt textbf ensuremath ref ref* mbox bibitem mathop " ++
"terminal literalterminal noncxxterminal textsl textsc textsf text term overline " ++
"tcode noncxxtcode literaltcode footnotetext microtypesetup cline mathtt mathit mathrm mathsf " ++
"label newlength uline value newcounter mathscr c uppercase iref operatorname " ++
"phantom hphantom sqrt ln emph minipage url indexescape changeglossnumformat textasciitilde " ++
"removedxref deprxref textsuperscript rlap mathrel mathbin nopnumdiffref color ucode uname")
, (2, "pnum definition addtocounter setcounter frac " ++
"binom infannex normannex parbox link weblink indexedspan movedxref movedxrefs " ++
"equal setlength textcolor")
, (3, "multicolumn discretionary movedxrefii ifthenelse PackageError")
, (4, "movedxrefiii indexlink hiddenindexlink")
]
initialCmds :: Map Text Command
initialCmds = Map.fromList $
[ storeCmd "item" (Signature 0 (Just []))
, storeCmd "caption" (Signature 2 (Just []))
, storeCmd "index" (Signature 2 (Just []))
, storeCmd "hyperref" (Signature 2 (Just []))
, nullCmd "makebox" (Signature 2 (Just []))
, storeCmd "\n" (Signature 0 Nothing)
, storeCmd "nolinebreak" (Signature 0 (Just []))
, storeCmd "textsmaller" (Signature 2 (Just []))
, nullCmd "gramSec" (Signature 2 (Just []))
, ("kern", normalCmd $ Command $ \_ctx _ws -> ParseResult [] mempty . snd . parseDimen)
]
++ [storeCmd c (Signature a Nothing) | (a, l) <- storeCmds, c <- words l]
++ [nullCmd (Text.pack c) (Signature a Nothing) | (a, l) <- nullCmds, c <- words l]
parseDimen :: [Token] -> ([Token], [Token])
parseDimen toks
| t@(Token txt) : more <- toks, txt `elem` [".", "pt", "-", "em"] || all isDigit txt = first (t :) (parseDimen more)
| otherwise = ([], toks)
initialEnvs :: Map Text Environment
initialEnvs = Map.fromList $
[ (storeEnv e (Signature 0 Nothing))
| e <- bnfEnvs ++
words "indented description itemize center tabbing defnote enumerate eqnarray* equation* itemdescr footnote matrix"
] ++
[ storeEnv "example" (Signature 1 (Just []))
, storeEnv "tailexample" (Signature 1 (Just []))
, storeEnv "note" (Signature 0 (Just [Token "Note"]))
, storeEnv "tailnote" (Signature 0 (Just [Token "Note"]))
, storeEnv "table" (Signature 1 Nothing)
, storeEnv "tabular" (Signature 1 Nothing)
, storeEnv "longtable" (Signature 1 Nothing)
, storeEnv "importgraphic" (Signature 3 Nothing)
, storeEnv "formula" (Signature 1 Nothing)
, storeEnv "minipage" (Signature 1 Nothing)
, storeEnv "thebibliography" (Signature 1 Nothing)
, codeEnv "indexeditemdecl" (Signature 1 Nothing)
, codeEnv "itemdecl" (Signature 0 Nothing)
, codeEnv "indexedcodeblock" (Signature 1 Nothing)
, codeEnv "codeblock" (Signature 0 Nothing)
, codeEnv "codeblockdigitsep" (Signature 0 Nothing)
, codeEnv "codeblocktu" (Signature 1 Nothing)
, storeEnv "array" (Signature 1 Nothing)
, storeEnv "floattablebasex" (Signature 4 Nothing)
, storeEnv "htmlTable" (Signature 3 Nothing)
]
initialMacros :: Parser.Macros
initialMacros = Parser.defaultMacros ++ mempty{Parser.commands=initialCmds, Parser.environments=initialEnvs}
initialContext :: Parser.Context
initialContext = Parser.defaultContext{Parser.macros=initialMacros}
parseFile :: Macros -> Text -> ([LinearSection], Macros)
parseFile macros =
first (parseSections 0)
. doParse macros
. replace "$$" "$"
. replace "\\hspace*" "\\hspace"
. replace "``" "“"
. textSubRegex (mkRegex "(\\grammarterm\\{[A-Za-z-]*\\})(\\{s\\}|s)") "\\1\\textit{s}"
-- Mixing italic and upright looks okay in the PDF, but looks bad in browsers,
-- and our linkification makes clear enough that the plural 's' is not part
-- of the grammarterm.
loadFigure :: Text -> Text
loadFigure f = unsafePerformIO $ do
dot <- readFile $ "assets/" ++ p
svg <- readProcess "dot" ["-Tsvg",
"-Gbgcolor=transparent",
"-Gsize=8",
"-Nfontsize=10",
"-Gfontsize=10",
"-Efontsize=10",
"-Nfontname=Noto Serif",
"-Efontname=Noto Serif",
"-Gfontname=Noto Serif"] (Text.unpack $ Text.replace "Courier New" "Noto Sans Mono" $ Text.replace ", fontsize=24" "" dot)
return $ rmIds $ snd $ Text.breakOn "<svg" $ Text.pack svg
where
p = Text.unpack $ Text.replace ".pdf" ".dot" f
r = mkRegex "<g id=\"[^\"]*\""
rmIds = textSubRegex r "<g"
-- Without rmIds, if a page has more than one figure, it will
-- have duplicate 'graph1', 'node1', 'edge1' etc ids.
isOnlySpace :: RawElement -> Bool
isOnlySpace (RawLatexElement x) = triml [x] == []
isOnlySpace _ = False
parsePara :: LaTeX -> [RawTexPara]
parsePara u = RawTexPara . dropWhile isOnlySpace . fmap f . splitElems (trim (filter (not . kill) u))
where
kill (TeXComm "hline" _ []) = True
kill (TeXComm "capsep" _ []) = True
kill (TeXComm "endhead" _ _) = True
kill _ = False
f :: LaTeXUnit -> RawElement
f e@(TeXEnv k a stuff)
| isFigure e
, [(FixArg, rawFigureName), (FixArg, [TeXRaw rawFigureAbbr]), (FixArg, [TeXRaw figureFile])] <- a
= RawFigure{rawFigureSvg=loadFigure figureFile, ..}
| k == "formula", [(FixArg, [TeXRaw rawFormulaAbbr])] <- a = RawFormula{rawFormulaContent = stuff, ..}
| isTable e
, ((_, cap) : (_, [TeXRaw abbr]) : (_, y) : _) <- a
= RawTable
{ rawTableCaption = cap
, rawColumnSpec = parseColspec y
, rawTableAbbr = "tab:" ++ abbr
, rawTableBody = breakMultiCols $ parseTable stuff }
| isTable e = error $ "other table: " ++ show e
| isTabbing e = RawTabbing stuff
| isBnf e = RawBnf (if "nc" `isPrefixOf` k then drop 2 k else k) stuff
| Just ek <- isEnumerate e = RawEnumerated ek (parseItems stuff)
| isCodeblock e = RawCodeblock e
| k `elem` ["note", "defnote", "tailnote"] =
let label = case a of [(FixArg, [TeXRaw x])] -> x; _ -> "Note"
in RawNote label $ parsePara stuff
| k `elem` ["example", "tailexample"] = RawExample $ parsePara stuff
| k == "itemdecl" || k == "minipage" || k == "indexeditemdecl" = RawLatexElement e
| k == "itemdescr" = RawItemdescr $ parsePara stuff
f x = RawLatexElement x
splitElems :: LaTeX -> [LaTeX]
splitElems [] = []
splitElems (x:xs)
| TeXRaw (textStripInfix "\n\n" -> Just (a, (Text.stripStart -> b))) <- x =
(if a == "" then ([] :) else ([TeXRaw a] :)) $
splitElems (if b == "" then xs else TeXRaw b : xs)
| otherwise = case splitElems xs of
[] -> [[x]]
a:b -> ((x:a):b)
class ExtractFootnotes a where extractFootnotes :: a -> (a, [RawFootnote])
instance ExtractFootnotes LaTeX where
extractFootnotes [] = ([], [])
extractFootnotes (TeXRaw x : t@(TeXEnv "footnote" _ _ : _))
= (TeXRaw (Text.stripEnd x) : t', ft)
where (t', ft) = extractFootnotes t
-- stripEnd here implements the footnote's \unskip
extractFootnotes (h:t) = (h' : t', fh ++ ft)
where
(h', fh) = extractFootnotes h
(t', ft) = extractFootnotes t
instance ExtractFootnotes LaTeXUnit where
extractFootnotes (TeXEnv "footnote" [] content) =
(TeXComm "footnoteref" "" [], [RawFootnote $ parsePara content])
extractFootnotes (TeXComm "footnotemark" _ []) =
(TeXComm "footnoteref" "" [], [])
extractFootnotes (TeXComm "footnotetext" _ [(_, content)]) =
(TeXRaw "" {- todo.. -}, [RawFootnote $ parsePara content])
extractFootnotes (TeXComm a ws [(FixArg, content)]) =
first (\c -> TeXComm a ws [(FixArg, c)]) (extractFootnotes content)
extractFootnotes (TeXEnv env args content) = first (TeXEnv env args) (extractFootnotes content)
extractFootnotes other = (other, [])
parseParas :: LaTeX -> ([RawParagraph], [RawFootnote], LaTeX {- rest -})
parseParas (break isParasEnd -> (extractFootnotes -> (stuff, fs), rest))
= (collectParas stuff, fs, rest)
where
collectParas :: LaTeX -> [RawParagraph]
collectParas (t@(TeXEnv "indexeditemdecl" _ _) : more) =
RawParagraph False False (parsePara [t]) Nothing : collectParas more
collectParas (t@(TeXEnv "itemdecl" _ _) : more) =
RawParagraph False False (parsePara [t]) Nothing : collectParas more
collectParas (TeXEnv "itemdescr" _ desc : more) =
map (\p -> p{rawParaInItemdescr=True}) (collectParas desc)
++ collectParas more
collectParas (TeXComm "pnum" _
[ (FixArg, [TeXRaw (Text.unpack -> file)])
, (FixArg, [TeXRaw (Text.unpack -> read -> lineNr)])] : more) =
(\(p : x) -> p{paraNumbered=True, rawParaSourceLoc=Just (SourceLocation file lineNr)} : x)
(collectParas more)
collectParas (TeXComm "pnum" _ [] : more) =
(\(p : x) -> p{paraNumbered=True, rawParaSourceLoc=Nothing} : x)
(collectParas more)
collectParas [] = []
collectParas x = (if null p then id else (RawParagraph False False p Nothing :)) (collectParas more)
where (parsePara -> p, more) = break isParaEnd x
parseSections :: Int -> LaTeX -> [LinearSection]
parseSections level
(TeXComm c _ args : (parseParas -> (lsectionParagraphs, lsectionFootnotes, more)))
| ((FixArg, isJustRaw -> fromJust -> lsectionAbbreviation), (FixArg, lsectionName), lsectionKind, level') <- case (c, args) of
("normannex", [abbr, name]) -> (abbr, name, NormativeAnnexSection, level)
("infannex", [abbr, name]) -> (abbr, name, InformativeAnnexSection, level)
("definition", [name, abbr]) -> (abbr, name, DefinitionSection (level + 1), level)
("rSec", [(FixArg, [TeXRaw (Text.unpack -> read -> l)]), abbr, name]) ->
(abbr, name, NormalSection l, l)
_ -> error $ "not a section command: " ++ show (c, args)
= LinearSection{..} : parseSections level' more
parseSections _ [] = []
parseSections l (x:xx)
| TeXRaw t <- x, all isSpace (Text.unpack t) = parseSections l xx
| otherwise = error $ "parseSections: " ++ show x
parseTable :: LaTeX -> [Row [RawTexPara]]
parseTable latex
| triml latex == [] = []
| triml row == [] = parseTable $ tail rest
| hasCommand (== "endfirsthead") row = parseTable $ findEndHead rest
| hasCommand (`elem` ["caption", "bottomline"]) row = parseTable rest
| otherwise = makeRow row : parseTable rest
where
(row, rest) = break (== TeXLineBreak) latex
findEndHead l
| row' == [] = findEndHead $ tail rest'
| hasCommand (== "endhead") row' = l
| otherwise = findEndHead rest'
where
(row', rest') = break (== TeXLineBreak) l
columnBreakCell :: Cell [RawTexPara]
columnBreakCell = Cell Normal [RawTexPara [RawLatexElement (TeXComm "columnbreak" "" [])]]
isColumnBreakCell :: Cell [RawTexPara] -> Bool
isColumnBreakCell (Cell Normal [RawTexPara [RawLatexElement (TeXComm "columnbreak" _ [])]]) = True
isColumnBreakCell _ = False
makeRectangular :: a -> [[a]] -> [[a]]
makeRectangular filler rows = (take numCols . (++ repeat filler)) . rows
where numCols = maximum (length . rows)
-- Todo: Remove this when the bugs in Chrome's collapsed border rendering are fixed.
breakMultiCols :: [Row [RawTexPara]] -> [Row [RawTexPara]]
-- implements the multicolfloattable environment's \columnbreak, which is left intact by parseTable
breakMultiCols rows
| all (\Row{..} -> length cells == 1 && rowSep == NoSep) rows =
Row NoSep . makeRectangular (Cell Normal []) (transpose $ splitOn isColumnBreakCell $ separateColumnBreaks $ (head . cells) . rows)
| otherwise = rows
where
separateColumnBreaks :: [Cell [RawTexPara]] -> [Cell [RawTexPara]]
separateColumnBreaks = concatMap f
where
f :: Cell [RawTexPara] -> [Cell [RawTexPara]]
f c@Cell{..} | [RawTexPara (RawLatexElement (TeXComm "columnbreak" _ []) : rest)] <- content =
[columnBreakCell, c{content = [RawTexPara rest]}]
| otherwise = [c]
makeRow :: LaTeX -> Row [RawTexPara]
makeRow l = Row sep $ makeRowCells l
where
sep
| hasCommand (== "hline") l = RowSep
| hasCommand (== "capsep") l = CapSep
| hasCommand (== "cline") l = Clines $ clines $ lookForCommand "cline" l
| otherwise = NoSep
clines [] = []
clines (([(FixArg, [TeXRaw c])]) : rest) = (begin, end) : clines rest
where
(begin', end') = Text.breakOn "-" c
begin = read $ Text.unpack begin' :: Int
end = read $ Text.unpack $ Text.tail end' :: Int
clines other = error $ "Unexpected \\clines syntax: " ++ show other
parseWidth :: LaTeX -> (Maybe Text, LaTeX)
parseWidth (TeXRaw "" : x) = parseWidth x
parseWidth (TeXBraces [TeXRaw x] : rest) = (Just x, rest)
parseWidth (TeXBraces [TeXRaw x, TeXComm "hsize" "" []] : rest) =
(Just $ Text.pack (show (round ((read ("0" ++ Text.unpack x) :: Double) * 100) :: Int)) ++ "%", rest)
parseWidth (TeXBraces _ : rest) = (Nothing, rest) -- remaining cases unsupported for now
parseWidth x = (Nothing, x)
parseColspec :: LaTeX -> [ColumnSpec]
parseColspec = \x -> case x of
[] -> []
TeXRaw (Text.unpack -> '|' : z) : y -> go (TeXRaw (Text.pack z) : y)
_ -> go x
where
go :: LaTeX -> [ColumnSpec]
go [] = []
go [TeXRaw "|"] = []
go (TeXRaw "@" : TeXBraces _ : x) = go x -- unimplemented
go (TeXRaw ">" : TeXBraces _ : x) = go x -- unimplemented
go (TeXRaw "" : y) = go y
go (TeXRaw (Text.uncons -> Just (letter, rest)) : y)
| letter == ' ' = go (TeXRaw rest : y)
| letter == '|' = mapHead (\(ColumnSpec x _ z) -> ColumnSpec x True z) $ go (TeXRaw rest : y)
| otherwise =
let (w, rest') = parseWidth (TeXRaw rest : y)
in ColumnSpec (colClass letter) False w : go rest'
go x = error ("parseColspec: " ++ show x)
colClass :: Char -> TextAlignment
colClass x | x `elem` ['l', 'm', 'x'] = AlignLeft
colClass 'p' = Justify
colClass 'r' = AlignRight
colClass 'c' = AlignCenter
colClass other = error $ "Unexpected column type " ++ (other : [])
makeRowCells :: LaTeX -> [Cell [RawTexPara]]
makeRowCells [] = []
makeRowCells latex =
case rest of
[] -> [makeCell cell]
_ : r ->
(makeCell $ cell <> [TeXRaw cell']) : makeRowCells (TeXRaw rest'' : r)
where
(cell, rest) = break isColEnd latex
isColEnd (TeXRaw c) = isJust $ Text.find (== '&') c
isColEnd _ = False
(cell', rest') = Text.break (== '&') $ getText rest
rest'' = Text.drop 1 rest'
getText (TeXRaw s : _) = s
getText other = error $ "Didn't expect " ++ show other
makeCell content
| [[(FixArg, [TeXRaw w]), (FixArg, cs), (FixArg, content')]] <- lookForCommand "multicolumn" content =
Cell (Multicolumn (read $ Text.unpack w) (head $ parseColspec cs)) $ parsePara content'
| otherwise =
Cell Normal $ parsePara content
rmExplSyntax :: Text -> Text
rmExplSyntax = Text.unlines . f . Text.lines
where
f [] = []
f ("\\ExplSyntaxOn" : (dropWhile (/= "\\ExplSyntaxOff") -> (_ : x))) = f x
f (h : t) = h : f t
loadMacros :: Text -> IO Macros
loadMacros extraMacros =
(initialMacros ++)
. snd
. doParse initialMacros
. replace "\\indeximpldef{" "\\index[impldefindex]{"
. textSubRegex (mkRegex "\\\\penalty[0-9]+{}") ""
. textSubRegex (mkRegex "\\\\verbtocs{[\\a-zA-Z]*}\\|[^|]*\\|") ""
. rmExplSyntax
. (++ extraMacros)
. mconcat
. mapM readFile
["config.tex", "macros.tex", "tables.tex"]
loadXrefDelta :: IO XrefDelta
loadXrefDelta = do
(tex, _, _) <- Parser.parseString initialContext . Text.unpack . readFile "xrefdelta.tex"
let lfc c = lookForCommand c tex
return $
[ (fromJust $ isJustRaw $ snd from, [snd to])
| [from, to] <- lfc "movedxrefs" ] ++
[ (fromJust $ isJustRaw $ snd from, (:[]) . TeXComm "ref" "" . (:[]) . tos)
| from : tos <- lfc "movedxref" ++ lfc "movedxrefii" ++ lfc "movedxrefiii" ] ++
[ (abbr, [])
| [(_, [TeXRaw abbr])] <- lfc "removedxref" ] ++
[ (abbr, [[TeXComm "ref" "" [(FixArg, [TeXRaw ("depr." ++ abbr)])]]])
| [(_, [TeXRaw abbr])] <- lfc "deprxref" ]