git.iko.soy/typeable/pandoc
on master help sign in
HTML reader: support column alignments.

These can be set either with a `width` attribute or with `text-width` in a `style` attribute. Closes #1881.

commitb1f6fb4af5e6df40fe72d6224512f60be082a8cd authorJohn MacFarlane <jgm@berkeley.edu> dateThu 17 Aug 2017, 12:07 (9 years ago) parentb9b3505
3 files changed+83−13
pandoc.cabal+1 src/Text/Pandoc/Readers/HTML.hs+30−13 test/command/1881.md+52
pandoc.cabal+1
@@ -301,6 +301,7 @@ Library
301301 HTTP >= 4000.0.5 && < 4000.4,
302302 texmath >= 0.9.4.1 && < 0.10,
303303 xml >= 1.3.12 && < 1.4,
304+ split >= 0.2 && < 0.3,
304305 random >= 1 && < 1.2,
305306 pandoc-types >= 1.17 && < 1.18,
306307 aeson >= 0.7 && < 1.3,
src/Text/Pandoc/Readers/HTML.hs+30−13
@@ -55,9 +55,10 @@ import Text.Pandoc.Walk
5555import qualified Data.Map as M
5656import Data.Foldable ( for_ )
5757import Data.Maybe ( fromMaybe, isJust, isNothing )
58+import Data.List.Split ( wordsBy )
5859import Data.List ( intercalate, isPrefixOf )
5960import Data.Char ( isDigit, isLetter, isAlphaNum )
60−import Control.Monad ( guard, mzero, void, unless )
61+import Control.Monad ( guard, mzero, void, unless, mplus )
6162import Control.Arrow ((***))
6263import Control.Applicative ( (<|>) )
6364import Data.Monoid (First (..))
@@ -472,31 +473,35 @@ pTable = try $ do
472473 caption <- option mempty $ pInTags "caption" inline <* skipMany pBlank
473474 widths' <- (mconcat <$> many1 pColgroup) <|> many pCol
474475 let pTh = option [] $ pInTags "tr" (pCell "th")
475− pTr = try $ skipMany pBlank >> pInTags "tr" (pCell "td" <|> pCell "th")
476+ pTr = try $ skipMany pBlank >>
477+ pInTags "tr" (pCell "td" <|> pCell "th")
476478 pTBody = do pOptInTag "tbody" $ many1 pTr
477479 head'' <- pOptInTag "thead" pTh
478− head' <- pOptInTag "tbody" $ do
479− if null head''
480− then pTh
481− else return head''
480+ head' <- map snd <$>
481+ (pOptInTag "tbody" $
482+ if null head'' then pTh else return head'')
482483 rowsLs <- many pTBody
483484 rows' <- pOptInTag "tfoot" $ many pTr
484485 TagClose _ <- pSatisfy (matchTagClose "table")
485486 let rows'' = (concat rowsLs) <> rows'
487+ let rows''' = map (map snd) rows''
488+ -- let rows''' = map (map snd) rows''
486489 -- fail on empty table
487− guard $ not $ null head' && null rows''
490+ guard $ not $ null head' && null rows'''
488491 let isSinglePlain x = case B.toList x of
489492 [] -> True
490493 [Plain _] -> True
491494 _ -> False
492− let isSimple = all isSinglePlain $ concat (head':rows'')
493− let cols = length $ if null head' then head rows'' else head'
495+ let isSimple = all isSinglePlain $ concat (head':rows''')
496+ let cols = length $ if null head' then head rows''' else head'
494497 -- add empty cells to short rows
495498 let addEmpties r = case cols - length r of
496499 n | n > 0 -> r <> replicate n mempty
497500 | otherwise -> r
498− let rows = map addEmpties rows''
499− let aligns = replicate cols AlignDefault
501+ let rows = map addEmpties rows'''
502+ let aligns = case rows'' of
503+ (cs:_) -> map fst cs
504+ _ -> replicate cols AlignDefault
500505 let widths = if null widths'
501506 then if isSimple
502507 then replicate cols 0
@@ -534,12 +539,24 @@ noColOrRowSpans t = isNullOrOne "colspan" && isNullOrOne "rowspan"
534539 "1" -> True
535540 _ -> False
536541
537−pCell :: PandocMonad m => Text -> TagParser m [Blocks]
542+pCell :: PandocMonad m => Text -> TagParser m [(Alignment, Blocks)]
538543pCell celltype = try $ do
539544 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
540557 res <- pInTags' celltype noColOrRowSpans block
541558 skipMany pBlank
542− return [res]
559+ return [(align, res)]
543560
544561pBlockQuote :: PandocMonad m => TagParser m Blocks
545562pBlockQuote = 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+