Org reader: support table.el tables

Closes #3314
This commit is contained in:
Albert Krewinkel 2017-05-03 22:43:34 +02:00
parent df23d96c89
commit 57cba3f1d5
No known key found for this signature in database
GPG key ID: 388DC0B21F631124
4 changed files with 52 additions and 8 deletions

View file

@ -61,6 +61,10 @@ headerStart = try $
tableStart :: Monad m => OrgParser m Char tableStart :: Monad m => OrgParser m Char
tableStart = try $ skipSpaces *> char '|' tableStart = try $ skipSpaces *> char '|'
gridTableStart :: Monad m => OrgParser m ()
gridTableStart = try $ skipSpaces <* char '+' <* char '-'
latexEnvStart :: Monad m => OrgParser m String latexEnvStart :: Monad m => OrgParser m String
latexEnvStart = try $ do latexEnvStart = try $ do
skipSpaces *> string "\\begin{" skipSpaces *> string "\\begin{"
@ -126,6 +130,7 @@ endOfBlock = lookAhead . try $ do
, hline , hline
, metaLineStart , metaLineStart
, commentLineStart , commentLineStart
, gridTableStart
, void noteMarker , void noteMarker
, void tableStart , void tableStart
, void drawerStart , void drawerStart

View file

@ -755,7 +755,11 @@ data OrgTable = OrgTable
} }
table :: PandocMonad m => OrgParser m (F Blocks) table :: PandocMonad m => OrgParser m (F Blocks)
table = try $ do table = gridTableWith blocks True <|> orgTable
-- | A normal org table
orgTable :: PandocMonad m => OrgParser m (F Blocks)
orgTable = try $ do
-- don't allow a table on the first line of a list item; org requires that -- don't allow a table on the first line of a list item; org requires that
-- tables start at first non-space character on the line -- tables start at first non-space character on the line
let isFirstInListItem st = (orgStateParserContext st == ListItemState) && let isFirstInListItem st = (orgStateParserContext st == ListItemState) &&
@ -854,28 +858,28 @@ normalizeTable (OrgTable colProps heads rows) =
rowToContent :: OrgTable rowToContent :: OrgTable
-> OrgTableRow -> OrgTableRow
-> F OrgTable -> F OrgTable
rowToContent orgTable row = rowToContent tbl row =
case row of case row of
OrgHlineRow -> return singleRowPromotedToHeader OrgHlineRow -> return singleRowPromotedToHeader
OrgAlignRow props -> return . setProperties $ props OrgAlignRow props -> return . setProperties $ props
OrgContentRow cs -> appendToBody cs OrgContentRow cs -> appendToBody cs
where where
singleRowPromotedToHeader :: OrgTable singleRowPromotedToHeader :: OrgTable
singleRowPromotedToHeader = case orgTable of singleRowPromotedToHeader = case tbl of
OrgTable{ orgTableHeader = [], orgTableRows = b:[] } -> OrgTable{ orgTableHeader = [], orgTableRows = b:[] } ->
orgTable{ orgTableHeader = b , orgTableRows = [] } tbl{ orgTableHeader = b , orgTableRows = [] }
_ -> orgTable _ -> tbl
setProperties :: [ColumnProperty] -> OrgTable setProperties :: [ColumnProperty] -> OrgTable
setProperties ps = orgTable{ orgTableColumnProperties = ps } setProperties ps = tbl{ orgTableColumnProperties = ps }
appendToBody :: F [Blocks] -> F OrgTable appendToBody :: F [Blocks] -> F OrgTable
appendToBody frow = do appendToBody frow = do
newRow <- frow newRow <- frow
let oldRows = orgTableRows orgTable let oldRows = orgTableRows tbl
-- NOTE: This is an inefficient O(n) operation. This should be changed -- NOTE: This is an inefficient O(n) operation. This should be changed
-- if performance ever becomes a problem. -- if performance ever becomes a problem.
return orgTable{ orgTableRows = oldRows ++ [newRow] } return tbl{ orgTableRows = oldRows ++ [newRow] }
-- --

View file

@ -70,6 +70,7 @@ module Text.Pandoc.Readers.Org.Parsing
, dash , dash
, ellipses , ellipses
, citeKey , citeKey
, gridTableWith
-- * Re-exports from Text.Pandoc.Parsec -- * Re-exports from Text.Pandoc.Parsec
, runParser , runParser
, runParserT , runParserT

34
test/command/3314.md Normal file
View file

@ -0,0 +1,34 @@
See #3315 and <http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#simple-tables>.
```
% pandoc -f org -t html5
+-----------+-------+----------+
| First | 12.0 | Example |
| | | row |
| | | spanning |
| | | lines |
+-----------+-------+----------+
| Second | 5.0 | Another |
+-----------+-------+----------+
^D
<table style="width:43%;">
<colgroup>
<col style="width: 16%" />
<col style="width: 11%" />
<col style="width: 15%" />
</colgroup>
<tbody>
<tr class="odd">
<td>First</td>
<td>12.0</td>
<td>Example row spanning lines</td>
</tr>
<tr class="even">
<td>Second</td>
<td>5.0</td>
<td>Another</td>
</tr>
</tbody>
</table>
```