git.iko.soy/typeable/pandoc
on master help sign in
Better handling of \part in LaTeX.

Closes #1905. Removed stateChapters from ParserState. Now we parse chapters as level 0 headers, and parts as level -1 headers. After parsing, we check for the lowest header level, and if it's less than 1 we bump everything up so that 1 is the lowest header level. So `\part` will always produce a header; no command-line options are needed.

commit6bf3f89d69f96f043f63600e199e53c8cfac680c authorJohn MacFarlane <jgm@berkeley.edu> dateMon 13 Mar 2017, 22:11 (10 years ago) parentd5086e7
4 files changed+48−10
pandoc.cabal+1 src/Text/Pandoc/Parsing.hs−2 src/Text/Pandoc/Readers/LaTeX.hs+17−8 test/command/1905.md+30
pandoc.cabal+1
@@ -270,6 +270,7 @@ Library
270270 directory >= 1 && < 1.4,
271271 bytestring >= 0.9 && < 0.11,
272272 text >= 0.11 && < 1.3,
273+ safe >= 0.3 && < 0.4,
273274 zip-archive >= 0.2.3.4 && < 0.4,
274275 HTTP >= 4000.0.5 && < 4000.4,
275276 texmath >= 0.9.3 && < 0.10,
src/Text/Pandoc/Parsing.hs−2
@@ -928,7 +928,6 @@ data ParserState = ParserState
928928 stateIdentifiers :: Set.Set String, -- ^ Header identifiers used
929929 stateNextExample :: Int, -- ^ Number of next example
930930 stateExamples :: M.Map String Int, -- ^ Map from example labels to numbers
931− stateHasChapters :: Bool, -- ^ True if \chapter encountered
932931 stateMacros :: [Macro], -- ^ List of macros defined so far
933932 stateRstDefaultRole :: String, -- ^ Current rST default interpreted text role
934933 stateRstCustomRoles :: M.Map String (String, Maybe String, Attr), -- ^ Current rST custom text roles
@@ -1036,7 +1035,6 @@ defaultParserState =
10361035 stateIdentifiers = Set.empty,
10371036 stateNextExample = 1,
10381037 stateExamples = M.empty,
1039− stateHasChapters = False,
10401038 stateMacros = [],
10411039 stateRstDefaultRole = "title-reference",
10421040 stateRstCustomRoles = M.empty,
src/Text/Pandoc/Readers/LaTeX.hs+17−8
@@ -42,6 +42,7 @@ import Data.Char (chr, isAlphaNum, isLetter, ord)
4242import Data.List (intercalate)
4343import qualified Data.Map as M
4444import Data.Maybe (fromMaybe, maybeToList)
45+import Safe (minimumDef)
4546import System.FilePath (addExtension, replaceExtension, takeExtension)
4647import Text.Pandoc.Builder
4748import Text.Pandoc.Class (PandocMonad, PandocPure, lookupEnv, readFileFromDirs,
@@ -72,7 +73,17 @@ parseLaTeX = do
7273 eof
7374 st <- getState
7475 let meta = stateMeta st
75− let (Pandoc _ bs') = doc bs
76+ let doc' = doc bs
77+ let headerLevel (Header n _ _) = [n]
78+ headerLevel _ = []
79+ let bottomLevel = minimumDef 1 $ query headerLevel doc'
80+ let adjustHeaders m (Header n attr ils) = Header (n+m) attr ils
81+ adjustHeaders _ x = x
82+ let (Pandoc _ bs') =
83+ -- handle the case where you have \part or \chapter
84+ (if bottomLevel < 1
85+ then walk (adjustHeaders (1 - bottomLevel))
86+ else id) doc'
7687 return $ Pandoc meta bs'
7788
7889type LP m = ParserT String ParserState m
@@ -345,10 +356,10 @@ blockCommands = M.fromList $
345356 -- Koma-script metadata commands
346357 , ("dedication", mempty <$ (skipopts *> tok >>= addMeta "dedication"))
347358 -- sectioning
348− , ("chapter", updateState (\s -> s{ stateHasChapters = True })
349− *> section nullAttr 0)
350− , ("chapter*", updateState (\s -> s{ stateHasChapters = True })
351− *> section ("",["unnumbered"],[]) 0)
359+ , ("part", section nullAttr (-1))
360+ , ("part*", section nullAttr (-1))
361+ , ("chapter", section nullAttr 0)
362+ , ("chapter*", section ("",["unnumbered"],[]) 0)
352363 , ("section", section nullAttr 1)
353364 , ("section*", section ("",["unnumbered"],[]) 1)
354365 , ("subsection", section nullAttr 2)
@@ -444,13 +455,11 @@ authors = try $ do
444455
445456section :: PandocMonad m => Attr -> Int -> LP m Blocks
446457section (ident, classes, kvs) lvl = do
447− hasChapters <- stateHasChapters `fmap` getState
448− let lvl' = if hasChapters then lvl + 1 else lvl
449458 skipopts
450459 contents <- grouped inline
451460 lab <- option ident $ try (spaces' >> controlSeq "label" >> spaces' >> braced)
452461 attr' <- registerHeader (lab, classes, kvs) contents
453− return $ headerWith attr' lvl' contents
462+ return $ headerWith attr' lvl contents
454463
455464inlineCommand :: PandocMonad m => LP m Inlines
456465inlineCommand = try $ do
test/command/1905.mdnew file+30
@@ -0,0 +1,30 @@
1+```
2+% pandoc -f latex-auto_identifiers -t html
3+\chapter{chapone}
4+\part{partone}
5+\chapter{chaptwo}
6+\section{secone}
7+^D
8+<h2>chapone</h2>
9+<h1>partone</h1>
10+<h2>chaptwo</h2>
11+<h3>secone</h3>
12+```
13+
14+```
15+% pandoc -f latex-auto_identifiers -t html
16+\chapter{chapone}
17+\chapter{chaptwo}
18+\section{secone}
19+^D
20+<h1>chapone</h1>
21+<h1>chaptwo</h1>
22+<h2>secone</h2>
23+```
24+
25+```
26+% pandoc -f latex-auto_identifiers -t html
27+\section{secone}
28+^D
29+<h1>secone</h1>
30+```