git.iko.soy/typeable/pandoc
on master help sign in
HTML reader: support column alignments.
b1f6fb4John MacFarlane
pandoc.cabal+1
@@ -301,6 +301,7 @@ Library
301 HTTP >= 4000.0.5 && < 4000.4,
301 HTTP >= 4000.0.5 && < 4000.4,
302 texmath >= 0.9.4.1 && < 0.10,
302 texmath >= 0.9.4.1 && < 0.10,
303 xml >= 1.3.12 && < 1.4,
303 xml >= 1.3.12 && < 1.4,
304+ split >= 0.2 && < 0.3,
304 random >= 1 && < 1.2,
305 random >= 1 && < 1.2,
305 pandoc-types >= 1.17 && < 1.18,
306 pandoc-types >= 1.17 && < 1.18,
306 aeson >= 0.7 && < 1.3,
307 aeson >= 0.7 && < 1.3,
src/Text/Pandoc/Readers/HTML.hs+30−13
@@ -55,9 +55,10 @@ import Text.Pandoc.Walk
55import qualified Data.Map as M
55import qualified Data.Map as M
56import Data.Foldable ( for_ )
56import Data.Foldable ( for_ )
57import Data.Maybe ( fromMaybe, isJust, isNothing )
57import Data.Maybe ( fromMaybe, isJust, isNothing )
58+import Data.List.Split ( wordsBy )
58import Data.List ( intercalate, isPrefixOf )
59import Data.List ( intercalate, isPrefixOf )
59import Data.Char ( isDigit, isLetter, isAlphaNum )
60import Data.Char ( isDigit, isLetter, isAlphaNum )
60−import Control.Monad ( guard, mzero, void, unless )
61+import Control.Monad ( guard, mzero, void, unless, mplus )
61import Control.Arrow ((***))
62import Control.Arrow ((***))
62import Control.Applicative ( (<|>) )
63import Control.Applicative ( (<|>) )
63import Data.Monoid (First (..))
64import Data.Monoid (First (..))
@@ -472,31 +473,35 @@ pTable = try $ do
472 caption <- option mempty $ pInTags "caption" inline <* skipMany pBlank
473 caption <- option mempty $ pInTags "caption" inline <* skipMany pBlank
473 widths' <- (mconcat <$> many1 pColgroup) <|> many pCol
474 widths' <- (mconcat <$> many1 pColgroup) <|> many pCol
474 let pTh = option [] $ pInTags "tr" (pCell "th")
475 let pTh = option [] $ pInTags "tr" (pCell "th")
476+ pTr = try $ skipMany pBlank >>
475− pTr = try $ skipMany pBlank >> pInTags "tr" (pCell "td" <|> pCell "th")
477+ pInTags "tr" (pCell "td" <|> pCell "th")
476 pTBody = do pOptInTag "tbody" $ many1 pTr
478 pTBody = do pOptInTag "tbody" $ many1 pTr
477 head'' <- pOptInTag "thead" pTh
479 head'' <- pOptInTag "thead" pTh
480+ head' <- map snd <$>
478− head' <- pOptInTag "tbody" $ do
481+ (pOptInTag "tbody" $
479− if null head''
480− then pTh
481− else return head''
482+ if null head'' then pTh else return head'')
482 rowsLs <- many pTBody
483 rowsLs <- many pTBody
483 rows' <- pOptInTag "tfoot" $ many pTr
484 rows' <- pOptInTag "tfoot" $ many pTr
484 TagClose _ <- pSatisfy (matchTagClose "table")
485 TagClose _ <- pSatisfy (matchTagClose "table")
485 let rows'' = (concat rowsLs) <> rows'
486 let rows'' = (concat rowsLs) <> rows'
487+ let rows''' = map (map snd) rows''
488+ -- let rows''' = map (map snd) rows''
486 -- fail on empty table
489 -- fail on empty table
487− guard $ not $ null head' && null rows''
490+ guard $ not $ null head' && null rows'''
488 let isSinglePlain x = case B.toList x of
491 let isSinglePlain x = case B.toList x of
489 [] -> True
492 [] -> True
490 [Plain _] -> True
493 [Plain _] -> True
491 _ -> False
494 _ -> False
492− let isSimple = all isSinglePlain $ concat (head':rows'')
495+ let isSimple = all isSinglePlain $ concat (head':rows''')
493− let cols = length $ if null head' then head rows'' else head'
496+ let cols = length $ if null head' then head rows''' else head'
494 -- add empty cells to short rows
497 -- add empty cells to short rows
495 let addEmpties r = case cols - length r of
498 let addEmpties r = case cols - length r of
496 n | n > 0 -> r <> replicate n mempty
499 n | n > 0 -> r <> replicate n mempty
497 | otherwise -> r
500 | otherwise -> r
498− let rows = map addEmpties rows''
501+ let rows = map addEmpties rows'''
502+ let aligns = case rows'' of
503+ (cs:_) -> map fst cs
499− let aligns = replicate cols AlignDefault
504+ _ -> replicate cols AlignDefault
500 let widths = if null widths'
505 let widths = if null widths'
501 then if isSimple
506 then if isSimple
502 then replicate cols 0
507 then replicate cols 0
@@ -534,12 +539,24 @@ noColOrRowSpans t = isNullOrOne "colspan" && isNullOrOne "rowspan"
534 "1" -> True
539 "1" -> True
535 _ -> False
540 _ -> False
536
541
537−pCell :: PandocMonad m => Text -> TagParser m [Blocks]
542+pCell :: PandocMonad m => Text -> TagParser m [(Alignment, Blocks)]
538pCell celltype = try $ do
543pCell celltype = try $ do
539 skipMany pBlank
544 skipMany pBlank
545+ tag <- lookAhead $
546+ pSatisfy (\t -> t ~== TagOpen celltype [] && noColOrRowSpans t)
547+ let extractAlign' [] = ""
548+ extractAlign' ("text-align":x:_) = x
549+ extractAlign' (_:xs) = extractAlign' xs
550+ let extractAlign = extractAlign' . wordsBy (`elem` [' ','\t',';',':'])
551+ let align = case maybeFromAttrib "align" tag `mplus`
552+ (extractAlign <$> maybeFromAttrib "style" tag) of
553+ Just "left" -> AlignLeft
554+ Just "right" -> AlignRight
555+ Just "center" -> AlignCenter
556+ _ -> AlignDefault
540 res <- pInTags' celltype noColOrRowSpans block
557 res <- pInTags' celltype noColOrRowSpans block
541 skipMany pBlank
558 skipMany pBlank
542− return [res]
559+ return [(align, res)]
543
560
544pBlockQuote :: PandocMonad m => TagParser m Blocks
561pBlockQuote :: PandocMonad m => TagParser m Blocks
545pBlockQuote = do
562pBlockQuote = do
test/command/1881.mdnew file+52
@@ -0,0 +1,52 @@
1+```
2+% pandoc -f html -t native
3+<table>
4+<caption>Demonstration of simple table syntax.</caption>
5+<thead>
6+<tr class="header">
7+<th align="right">Right</th>
8+<th align="left">Left</th>
9+<th align="center">Center</th>
10+<th>Default</th>
11+</tr>
12+</thead>
13+<tbody>
14+<tr class="odd">
15+<td align="right">12</td>
16+<td align="left">12</td>
17+<td align="center">12</td>
18+<td>12</td>
19+</tr>
20+</tbody>
21+</table>
22+^D
23+[Table [Str "Demonstration",Space,Str "of",Space,Str "simple",Space,Str "table",Space,Str "syntax."] [AlignRight,AlignLeft,AlignCenter,AlignDefault] [0.0,0.0,0.0,0.0]
24+ [[Plain [Str "Right"]]
25+ ,[Plain [Str "Left"]]
26+ ,[Plain [Str "Center"]]
27+ ,[Plain [Str "Default"]]]
28+ [[[Plain [Str "12"]]
29+ ,[Plain [Str "12"]]
30+ ,[Plain [Str "12"]]
31+ ,[Plain [Str "12"]]]]]
32+```
33+
34+```
35+% pandoc -f html -t native
36+<table>
37+<tr class="odd">
38+<td style="text-align: right;">12</td>
39+<td style="text-align:left;">12</td>
40+<td style="text-align: center">12</td>
41+<td style="text-align: right;">12</td>
42+</tr>
43+</table>
44+^D
45+[Table [] [AlignRight,AlignLeft,AlignCenter,AlignRight] [0.0,0.0,0.0,0.0]
46+ []
47+ [[[Plain [Str "12"]]
48+ ,[Plain [Str "12"]]
49+ ,[Plain [Str "12"]]
50+ ,[Plain [Str "12"]]]]]
51+```
52+