git.iko.soy/typeable/pandoc
on master help sign in
Support GFM extensions in GFM reader.
commit43506477bd27546f176af4cc9be90688b4c57290 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 3 Aug 2017, 12:05 (9 years ago) parentb22bc02
1 file changed+30−7
src/Text/Pandoc/Readers/GFM.hs+30−7
src/Text/Pandoc/Readers/GFM.hs+30−7
@@ -38,16 +38,13 @@ import Data.Text (Text, unpack)
3838import Text.Pandoc.Class (PandocMonad)
3939import Text.Pandoc.Definition
4040import Text.Pandoc.Options
41−
41+import Debug.Trace
4242-- | Parse a CommonMark formatted string into a 'Pandoc' structure.
4343readGFM :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
4444readGFM opts s = return $
45− nodeToPandoc $ commonmarkToNode opts' exts s
45+ nodeToPandoc $ traceShowId $ commonmarkToNode opts' exts s
4646 where opts' = [optSmart | enabled Ext_smart]
47− exts = [extStrikethrough | enabled Ext_strikeout] ++
48− [extTable | enabled Ext_pipe_tables] ++
49− [extAutolink | enabled Ext_autolink_bare_uris]
50− -- extTagfilter: no pandoc extension for this
47+ exts = [extStrikethrough, extTable, extAutolink]
5148 enabled x = extensionEnabled x (readerExtensions opts)
5249
5350nodeToPandoc :: Node -> Pandoc
@@ -91,7 +88,31 @@ addBlock (Node _ (LIST listAttrs) nodes) =
9188 delim = case listDelim listAttrs of
9289 PERIOD_DELIM -> Period
9390 PAREN_DELIM -> OneParen
94−addBlock (Node _ ITEM _) = id -- handled in LIST
91+addBlock (Node _ (TABLE alignments) nodes) = do
92+ (Table [] aligns widths headers rows :)
93+ where aligns = map fromTableCellAlignment alignments
94+ fromTableCellAlignment None = AlignDefault
95+ fromTableCellAlignment CMarkGFM.Left = AlignLeft
96+ fromTableCellAlignment CMarkGFM.Right = AlignRight
97+ fromTableCellAlignment Center = AlignCenter
98+ widths = replicate numcols 0.0
99+ numcols = if null rows'
100+ then 0
101+ else maximum $ map length rows'
102+ rows' = map toRow $ filter isRow nodes
103+ (headers, rows) = case rows' of
104+ (h:rs) -> (h, rs)
105+ [] -> ([], [])
106+ isRow (Node _ TABLE_ROW _) = True
107+ isRow _ = False
108+ isCell (Node _ TABLE_CELL _) = True
109+ isCell _ = False
110+ toRow (Node _ TABLE_ROW ns) = map toCell $ filter isCell ns
111+ toRow (Node _ t _) = error $ "toRow encountered non-row " ++ show t
112+ toCell (Node _ TABLE_CELL ns) = addBlocks ns
113+ toCell (Node _ t _) = error $ "toCell encountered non-cell " ++ show t
114+addBlock (Node _ TABLE_ROW _) = id -- handled in TABLE
115+addBlock (Node _ TABLE_CELL _) = id -- handled in TABLE
95116addBlock _ = id
96117
97118children :: Node -> [Node]
@@ -124,6 +145,8 @@ addInline (Node _ EMPH nodes) =
124145 (Emph (addInlines nodes) :)
125146addInline (Node _ STRONG nodes) =
126147 (Strong (addInlines nodes) :)
148+addInline (Node _ STRIKETHROUGH nodes) =
149+ (Strikeout (addInlines nodes) :)
127150addInline (Node _ (LINK url title) nodes) =
128151 (Link nullAttr (addInlines nodes) (unpack url, unpack title) :)
129152addInline (Node _ (IMAGE url title) nodes) =