Skip to content

Commit

Permalink
Textile reader: Improved table support.
Browse files Browse the repository at this point in the history
We can now handle all different alignment types, for simple
tables only (no captions, no relative widths, cell contents just
plain inlines).  Other tables are still handled using raw HTML.

Addresses #1585 as far as it can be addresssed, I believe.
  • Loading branch information
jgm committed Aug 31, 2014
1 parent b509275 commit a827300
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 56 deletions.
17 changes: 13 additions & 4 deletions src/Text/Pandoc/Writers/Textile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import Text.Pandoc.Templates (renderTemplate')
import Text.Pandoc.XML ( escapeStringForXML )
import Data.List ( intercalate )
import Control.Monad.State
import Control.Applicative ((<$>))
import Data.Char ( isSpace )

data WriterState = WriterState {
Expand Down Expand Up @@ -164,14 +165,22 @@ blockToTextile opts (BlockQuote blocks) = do
return $ "<blockquote>\n\n" ++ contents ++ "\n</blockquote>\n"

blockToTextile opts (Table [] aligns widths headers rows') |
all (==0) widths && all (`elem` [AlignLeft,AlignDefault]) aligns = do
all (==0) widths = do
hs <- mapM (liftM (("_. " ++) . stripTrailingNewlines) . blockListToTextile opts) headers
let cellsToRow cells = "|" ++ intercalate "|" cells ++ "|"
let header = if all null headers then "" else cellsToRow hs
let rowToCells = mapM (liftM stripTrailingNewlines . blockListToTextile opts)
let header = if all null headers then "" else cellsToRow hs ++ "\n"
let blocksToCell (align, bs) = do
contents <- stripTrailingNewlines <$> blockListToTextile opts bs
let alignMarker = case align of
AlignLeft -> "<. "
AlignRight -> ">. "
AlignCenter -> "=. "
AlignDefault -> ""
return $ alignMarker ++ contents
let rowToCells = mapM blocksToCell . zip aligns
bs <- mapM rowToCells rows'
let body = unlines $ map cellsToRow bs
return $ header ++ "\n" ++ body ++ "\n"
return $ header ++ body

blockToTextile opts (Table capt aligns widths headers rows') = do
let alignStrings = map alignmentToString aligns
Expand Down
59 changes: 7 additions & 52 deletions tests/tables.textile
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,10 @@ Simple table with caption:

Simple table without caption:

<table>
<thead>
<tr class="header">
<th align="right">Right</th>
<th align="left">Left</th>
<th align="center">Center</th>
<th align="left">Default</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="right">12</td>
<td align="left">12</td>
<td align="center">12</td>
<td align="left">12</td>
</tr>
<tr class="even">
<td align="right">123</td>
<td align="left">123</td>
<td align="center">123</td>
<td align="left">123</td>
</tr>
<tr class="odd">
<td align="right">1</td>
<td align="left">1</td>
<td align="center">1</td>
<td align="left">1</td>
</tr>
</tbody>
</table>
|_. Right|_. Left|_. Center|_. Default|
|>. 12|<. 12|=. 12|12|
|>. 123|<. 123|=. 123|123|
|>. 1|<. 1|=. 1|1|

Simple table indented two spaces:

Expand Down Expand Up @@ -164,28 +138,9 @@ Multiline table without caption:

Table without column headers:

<table>
<tbody>
<tr class="odd">
<td align="right">12</td>
<td align="left">12</td>
<td align="center">12</td>
<td align="right">12</td>
</tr>
<tr class="even">
<td align="right">123</td>
<td align="left">123</td>
<td align="center">123</td>
<td align="right">123</td>
</tr>
<tr class="odd">
<td align="right">1</td>
<td align="left">1</td>
<td align="center">1</td>
<td align="right">1</td>
</tr>
</tbody>
</table>
|>. 12|<. 12|=. 12|>. 12|
|>. 123|<. 123|=. 123|>. 123|
|>. 1|<. 1|=. 1|>. 1|

Multiline table without column headers:

Expand Down

0 comments on commit a827300

Please sign in to comment.