git.iko.soy/typeable/pandoc
on master help sign in
Revised code for pipe tables.
d2cc56aJohn MacFarlane
pandoc.cabal+2−2
@@ -175,8 +175,8 @@ Extra-Source-Files:
175 tests/lhs-test.html,
175 tests/lhs-test.html,
176 tests/lhs-test.html+lhs,
176 tests/lhs-test.html+lhs,
177 tests/lhs-test.fragment.html+lhs,
177 tests/lhs-test.fragment.html+lhs,
178− tests/extra-tables.markdown,
178+ tests/pipe-tables.txt,
179− tests/extra-tables.native
179+ tests/pipe-tables.native
180Extra-Tmp-Files: man/man1/pandoc.1,
180Extra-Tmp-Files: man/man1/pandoc.1,
181 man/man5/pandoc_markdown.5
181 man/man5/pandoc_markdown.5
182
182
src/Tests/Old.hs+2−2
@@ -56,8 +56,8 @@ tests = [ testGroup "markdown"
56 "testsuite.txt" "testsuite.native"
56 "testsuite.txt" "testsuite.native"
57 , test "tables" ["-r", "markdown", "-w", "native", "--columns=80"]
57 , test "tables" ["-r", "markdown", "-w", "native", "--columns=80"]
58 "tables.txt" "tables.native"
58 "tables.txt" "tables.native"
59− , test "extratables" ["-r", "markdown", "-w", "native", "--columns=80"]
59+ , test "pipe tables" ["-r", "markdown", "-w", "native", "--columns=80"]
60− "extra-tables.markdown" "extra-tables.native"
60+ "pipe-tables.txt" "pipe-tables.native"
61 , test "more" ["-r", "markdown", "-w", "native", "-S"]
61 , test "more" ["-r", "markdown", "-w", "native", "-S"]
62 "markdown-reader-more.txt" "markdown-reader-more.native"
62 "markdown-reader-more.txt" "markdown-reader-more.native"
63 , lhsReaderTest "markdown+lhs"
63 , lhsReaderTest "markdown+lhs"
src/Text/Pandoc/Parsing.hs+4−94
@@ -57,7 +57,6 @@ module Text.Pandoc.Parsing ( (>>~),
57 orderedListMarker,
57 orderedListMarker,
58 charRef,
58 charRef,
59 tableWith,
59 tableWith,
60− extraTableWith,
61 gridTableWith,
60 gridTableWith,
62 readWith,
61 readWith,
63 testStringWith,
62 testStringWith,
@@ -108,9 +107,11 @@ module Text.Pandoc.Parsing ( (>>~),
108 (<?>),
107 (<?>),
109 choice,
108 choice,
110 try,
109 try,
111− sepBy1,
112 sepBy,
110 sepBy,
111+ sepBy1,
113 sepEndBy,
112 sepEndBy,
113+ sepEndBy1,
114+ endBy,
114 endBy1,
115 endBy1,
115 option,
116 option,
116 optional,
117 optional,
@@ -536,7 +537,7 @@ tableWith :: Parsec [Char] ParserState ([[Block]], [Alignment], [Int])
536tableWith headerParser rowParser lineParser footerParser captionParser = try $ do
537tableWith headerParser rowParser lineParser footerParser captionParser = try $ do
537 caption' <- option [] captionParser
538 caption' <- option [] captionParser
538 (heads, aligns, indices) <- headerParser
539 (heads, aligns, indices) <- headerParser
539− lines' <- rowParser indices `sepEndBy` lineParser
540+ lines' <- rowParser indices `sepEndBy1` lineParser
540 footerParser
541 footerParser
541 caption <- if null caption'
542 caption <- if null caption'
542 then option [] captionParser
543 then option [] captionParser
@@ -573,97 +574,6 @@ widthsFromIndices numColumns' indices =
573 fracs = map (\l -> (fromIntegral l) / quotient) lengths in
574 fracs = map (\l -> (fromIntegral l) / quotient) lengths in
574 tail fracs
575 tail fracs
575
576
576−
577−-- Parse an extra table (php-markdown): each line starts and ends with '|',
578−-- with a mandatory line of '--' to separate the (optionnal) headers from content.
579−extraTableWith :: GenParser Char ParserState Block -- ^ Block parser
580− -> GenParser Char ParserState [Inline] -- ^ Caption parser
581− -> Bool -- ^ Headerless table
582− -> GenParser Char ParserState Block
583−extraTableWith block tableCaption headless =
584− tableWith (extraTableHeader headless block) (extraTableRow block) (extraTableSep '-') extraTableFooter tableCaption
585−
586−-- | Parse header for an extra table.
587−extraTableHeader :: Bool -- ^ Headerless table
588− -> GenParser Char ParserState Block
589− -> GenParser Char ParserState ([[Block]], [Alignment], [Int])
590−extraTableHeader headless block = try $ do
591− optional blanklines
592− rawContent <- if headless
593− then return $ repeat ""
594− else many1
595− (notFollowedBy (extraTableHeaderSep) >> char '|' >>
596− many1Till anyChar newline)
597− aligns <- extraTableHeaderDashedLine
598− let indices = []
599− let rawHeads = if headless
600− then replicate (length aligns) ""
601− else map (intercalate " ") $ transpose
602− $ map (extraTableSplitLine )
603− $ map (trimOnceBy '|') rawContent
604− heads <- mapM (parseFromString $ many block) $
605− map removeLeadingTrailingSpace rawHeads
606− return (heads, aligns, indices)
607−
608−extraTableHeaderPart :: GenParser Char st Alignment
609−extraTableHeaderPart = do
610− left <- optionMaybe (char ':')
611− many1 (char '-')
612− right <- optionMaybe (char ':')
613− char '|'
614− return $
615− case (left,right) of
616− (Nothing,Nothing) -> AlignDefault
617− (Just _,Nothing) -> AlignLeft
618− (Nothing,Just _) -> AlignRight
619− (Just _,Just _) -> AlignCenter
620−
621−extraTableHeaderDashedLine :: GenParser Char st [Alignment]
622−extraTableHeaderDashedLine = try $ char '|' >> many1 (extraTableHeaderPart) >>~ blankline
623−
624−extraTableHeaderSep :: GenParser Char ParserState Char
625−extraTableHeaderSep = try $ extraTableHeaderDashedLine >> return '\n'
626−
627−-- | Split a header or data line in an extra table.
628−-- | The line must contain only *inside* separators.
629−extraTableSplitLine :: String -> [String]
630−extraTableSplitLine line = map removeLeadingSpace $
631− splitBy (== '|') $ removeTrailingSpace line
632−
633−-- Remove, if present, a character from both ends of a string
634−trimOnceBy :: Char -> String -> String
635−trimOnceBy ch s =
636− if (head s == ch) && (last s == ch)
637− then init $ tail s
638− else s
639−trimEndOnceBy :: Char -> String -> String
640−trimEndOnceBy ch s =
641− if (last s == ch)
642− then init s
643− else s
644−
645−-- | Parse row of an extra table.
646−extraTableRow :: GenParser Char ParserState Block
647− -> [Int]
648− -> GenParser Char ParserState [[Block]]
649−extraTableRow block indices = do
650− cols <- extraTableRawLine
651− mapM (liftM compactifyCell . parseFromString (many block)) cols
652−
653−extraTableRawLine :: GenParser Char ParserState [String]
654−extraTableRawLine = do
655− char '|'
656− line <- many1Till anyChar newline
657− return (extraTableSplitLine $ trimEndOnceBy '|' line)
658−
659−-- | Separator between rows of an extra table.
660−extraTableSep :: Char -> GenParser Char ParserState Char
661−extraTableSep ch = do return '\n'
662−
663−-- | Parse footer for an extra table.
664−extraTableFooter :: GenParser Char ParserState [Char]
665−extraTableFooter = blanklines
666−
667---
577---
668
578
669-- Parse a grid table: starts with row of '-' on top, then header
579-- Parse a grid table: starts with row of '-' on top, then header
src/Text/Pandoc/Readers/Markdown.hs+51−5
@@ -901,18 +901,64 @@ alignType strLst len =
901 (True, True) -> AlignCenter
901 (True, True) -> AlignCenter
902 (False, False) -> AlignDefault
902 (False, False) -> AlignDefault
903
903
904−extraTable :: Bool -- ^ Headerless table
905− -> GenParser Char ParserState Block
906−extraTable = extraTableWith block tableCaption
907−
908gridTable :: Bool -- ^ Headerless table
904gridTable :: Bool -- ^ Headerless table
909 -> Parser [Char] ParserState Block
905 -> Parser [Char] ParserState Block
910gridTable = gridTableWith block tableCaption
906gridTable = gridTableWith block tableCaption
911
907
908+pipeTable :: Bool -- ^ Headerless table
909+ -> Parser [Char] ParserState Block
910+pipeTable headless = tableWith (pipeTableHeader headless)
911+ (\_ -> pipeTableRow) (return ()) blanklines tableCaption
912+
913+-- | Parse header for an pipe table.
914+pipeTableHeader :: Bool -- ^ Headerless table
915+ -> Parser [Char] ParserState ([[Block]], [Alignment], [Int])
916+pipeTableHeader headless = try $ do
917+ optional blanklines
918+ heads <- if headless
919+ then return $ repeat []
920+ else pipeTableRow
921+ aligns <- nonindentSpaces >> optional (char '|') >>
922+ pipeTableHeaderPart `sepBy1` sepPipe
923+ optional (char '|')
924+ newline
925+ let cols = length aligns
926+ return (take cols heads, aligns, [])
927+
928+sepPipe :: Parser [Char] ParserState ()
929+sepPipe = try $ char '|' >> notFollowedBy blankline
930+
931+pipeTableRow :: Parser [Char] ParserState [[Block]]
932+pipeTableRow = do
933+ nonindentSpaces
934+ optional (char '|')
935+ let cell = many (notFollowedBy (blankline <|> char '|') >> inline)
936+ first <- cell
937+ sepPipe
938+ rest <- cell `sepBy1` sepPipe
939+ optional (char '|')
940+ blankline
941+ return $ map (\ils ->
942+ if null ils
943+ then []
944+ else [Plain $ normalizeSpaces ils]) (first:rest)
945+
946+pipeTableHeaderPart :: Parser [Char] st Alignment
947+pipeTableHeaderPart = do
948+ left <- optionMaybe (char ':')
949+ many1 (char '-')
950+ right <- optionMaybe (char ':')
951+ return $
952+ case (left,right) of
953+ (Nothing,Nothing) -> AlignDefault
954+ (Just _,Nothing) -> AlignLeft
955+ (Nothing,Just _) -> AlignRight
956+ (Just _,Just _) -> AlignCenter
957+
912table :: Parser [Char] ParserState Block
958table :: Parser [Char] ParserState Block
913table = multilineTable False <|> simpleTable True <|>
959table = multilineTable False <|> simpleTable True <|>
914 simpleTable False <|> multilineTable True <|>
960 simpleTable False <|> multilineTable True <|>
915− extraTable False <|> extraTable True <|>
961+ pipeTable False <|> pipeTable True <|>
916 gridTable False <|> gridTable True <?> "table"
962 gridTable False <|> gridTable True <?> "table"
917
963
918--
964--
tests/extra-tables.native → tests/pipe-tables.nativerenamed+11−1
@@ -57,4 +57,14 @@
57 ,[Plain [Str "123"]]]
57 ,[Plain [Str "123"]]]
58 ,[[Plain [Str "1"]]
58 ,[[Plain [Str "1"]]
59 ,[Plain [Str "1"]]
59 ,[Plain [Str "1"]]
60− ,[Plain [Str "1"]]]]]
60+ ,[Plain [Str "1"]]]]
61+,Para [Str "Table",Space,Str "without",Space,Str "sides",Str ":"]
62+,Table [] [AlignDefault,AlignRight] [0.0,0.0]
63+ [[Plain [Str "Fruit"]]
64+ ,[Plain [Str "Quantity"]]]
65+ [[[Plain [Str "apple"]]
66+ ,[Plain [Str "5"]]]
67+ ,[[Plain [Str "orange"]]
68+ ,[Plain [Str "17"]]]
69+ ,[[Plain [Str "pear"]]
70+ ,[Plain [Str "302"]]]]]
tests/extra-tables.markdown → tests/pipe-tables.txtrenamed+8
@@ -32,3 +32,11 @@ Headerless table without caption:
32|123|123|123|
32|123|123|123|
33|1|1|1|
33|1|1|1|
34
34
35+Table without sides:
36+
37+Fruit |Quantity
38+------|-------:
39+apple | 5
40+orange| 17
41+pear | 302
42+