git.iko.soy/typeable/pandoc
on master help sign in
MediaWiki writer: fixed headerless tables.

git-svn-id: https://pandoc.googlecode.com/svn/trunk@1878 788f1e2b-df1e-0410-8736-df70ead52e1b

commitea6721944903122b53c657b4901dbf687a1e416a authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateThu 11 Mar 2010, 03:01 (17 years ago) parentdf9d79c
2 files changed+152−62
src/Text/Pandoc/Writers/MediaWiki.hs+37−36 tests/tables.mediawiki+115−26
src/Text/Pandoc/Writers/MediaWiki.hs+37−36
@@ -118,17 +118,27 @@ blockToMediaWiki opts (BlockQuote blocks) = do
118118 contents <- blockListToMediaWiki opts blocks
119119 return $ "<blockquote>" ++ contents ++ "</blockquote>"
120120
121−blockToMediaWiki opts (Table caption aligns widths headers rows) = do
121+blockToMediaWiki opts (Table capt aligns widths headers rows') = do
122122 let alignStrings = map alignmentToString aligns
123− captionDoc <- if null caption
123+ captionDoc <- if null capt
124124 then return ""
125125 else do
126− c <- inlineListToMediaWiki opts caption
127− return $ "<caption>" ++ c ++ "</caption>"
128− colHeads <- colHeadsToMediaWiki opts alignStrings widths headers
129− rows' <- mapM (tableRowToMediaWiki opts alignStrings) rows
130− return $ "<table>\n" ++ captionDoc ++ colHeads ++ vcat rows' ++ "\n</table>"
131−
126+ c <- inlineListToMediaWiki opts capt
127+ return $ "<caption>" ++ c ++ "</caption>\n"
128+ let percent w = show (truncate (100*w) :: Integer) ++ "%"
129+ let coltags = if all (== 0.0) widths
130+ then ""
131+ else unlines $ map
132+ (\w -> "<col width=\"" ++ percent w ++ "\" />") widths
133+ head' <- if all null headers
134+ then return ""
135+ else do
136+ hs <- tableRowToMediaWiki opts alignStrings 0 headers
137+ return $ "<thead>\n" ++ hs ++ "\n</thead>\n"
138+ body' <- zipWithM (tableRowToMediaWiki opts alignStrings) [1..] rows'
139+ return $ "<table>\n" ++ captionDoc ++ coltags ++ head' ++
140+ "<tbody>\n" ++ unlines body' ++ "</tbody>\n</table>\n"
141+
132142blockToMediaWiki opts x@(BulletList items) = do
133143 oldUseTags <- get >>= return . stUseTags
134144 let useTags = oldUseTags || not (isSimpleList x)
@@ -249,25 +259,27 @@ isPlainOrPara (Plain _) = True
249259isPlainOrPara (Para _) = True
250260isPlainOrPara _ = False
251261
252−tr :: String -> String
253−tr x = "<tr>\n" ++ x ++ "\n</tr>"
254−
255262-- | Concatenates strings with line breaks between them.
256263vcat :: [String] -> String
257264vcat = intercalate "\n"
258265
259266-- Auxiliary functions for tables:
260267
261−colHeadsToMediaWiki :: WriterOptions
262− -> [[Char]]
263− -> [Double]
268+tableRowToMediaWiki :: WriterOptions
269+ -> [String]
270+ -> Int
264271 -> [[Block]]
265272 -> State WriterState String
266−colHeadsToMediaWiki opts alignStrings widths headers = do
267− heads <- sequence $ zipWith3
268− (\alignment columnwidth item -> tableItemToMediaWiki opts "th" alignment columnwidth item)
269− alignStrings widths headers
270− return $ tr $ vcat heads
273+tableRowToMediaWiki opts alignStrings rownum cols' = do
274+ let celltype = if rownum == 0 then "th" else "td"
275+ let rowclass = case rownum of
276+ 0 -> "header"
277+ x | x `rem` 2 == 1 -> "odd"
278+ _ -> "even"
279+ cols'' <- sequence $ zipWith
280+ (\alignment item -> tableItemToMediaWiki opts celltype alignment item)
281+ alignStrings cols'
282+ return $ "<tr class=\"" ++ rowclass ++ "\">\n" ++ unlines cols'' ++ "</tr>"
271283
272284alignmentToString :: Alignment -> [Char]
273285alignmentToString alignment = case alignment of
@@ -276,27 +288,16 @@ alignmentToString alignment = case alignment of
276288 AlignCenter -> "center"
277289 AlignDefault -> "left"
278290
279−tableRowToMediaWiki :: WriterOptions
280− -> [[Char]]
281− -> [[Block]]
282− -> State WriterState String
283−tableRowToMediaWiki opts aligns columns =
284− (sequence $ zipWith3 (tableItemToMediaWiki opts "td") aligns (repeat 0) columns) >>=
285− return . tr . vcat
286−
287291tableItemToMediaWiki :: WriterOptions
288− -> [Char]
289− -> [Char]
290− -> Double
292+ -> String
293+ -> String
291294 -> [Block]
292295 -> State WriterState String
293−tableItemToMediaWiki opts tag' align' width' item = do
296+tableItemToMediaWiki opts celltype align' item = do
297+ let mkcell x = "<" ++ celltype ++ " align=\"" ++ align' ++ "\">" ++
298+ x ++ "</" ++ celltype ++ ">"
294299 contents <- blockListToMediaWiki opts item
295− let attrib = " align=\"" ++ align' ++ "\"" ++
296− if width' /= 0
297− then " style=\"width: " ++ (show (truncate (100 * width') :: Integer)) ++ "%;\""
298− else ""
299− return $ "<" ++ tag' ++ attrib ++ ">" ++ contents ++ "</" ++ tag' ++ ">"
300+ return $ mkcell contents
300301
301302-- | Convert list of Pandoc block elements to MediaWiki.
302303blockListToMediaWiki :: WriterOptions -- ^ Options
tests/tables.mediawiki+115−26
@@ -1,123 +1,212 @@
11Simple table with caption:
22
33<table>
4−<caption>Demonstration of simple table syntax.</caption><tr>
4+<caption>Demonstration of simple table syntax.</caption>
5+<thead>
6+<tr class="header">
57<th align="right">Right</th>
68<th align="left">Left</th>
79<th align="center">Center</th>
810<th align="left">Default</th>
9−</tr><tr>
11+</tr>
12+</thead>
13+<tbody>
14+<tr class="odd">
1015<td align="right">12</td>
1116<td align="left">12</td>
1217<td align="center">12</td>
1318<td align="left">12</td>
1419</tr>
15−<tr>
20+<tr class="even">
1621<td align="right">123</td>
1722<td align="left">123</td>
1823<td align="center">123</td>
1924<td align="left">123</td>
2025</tr>
21−<tr>
26+<tr class="odd">
2227<td align="right">1</td>
2328<td align="left">1</td>
2429<td align="center">1</td>
2530<td align="left">1</td>
2631</tr>
32+</tbody>
2733</table>
34+
2835Simple table without caption:
2936
3037<table>
31−<tr>
38+<thead>
39+<tr class="header">
3240<th align="right">Right</th>
3341<th align="left">Left</th>
3442<th align="center">Center</th>
3543<th align="left">Default</th>
36−</tr><tr>
44+</tr>
45+</thead>
46+<tbody>
47+<tr class="odd">
3748<td align="right">12</td>
3849<td align="left">12</td>
3950<td align="center">12</td>
4051<td align="left">12</td>
4152</tr>
42−<tr>
53+<tr class="even">
4354<td align="right">123</td>
4455<td align="left">123</td>
4556<td align="center">123</td>
4657<td align="left">123</td>
4758</tr>
48−<tr>
59+<tr class="odd">
4960<td align="right">1</td>
5061<td align="left">1</td>
5162<td align="center">1</td>
5263<td align="left">1</td>
5364</tr>
65+</tbody>
5466</table>
67+
5568Simple table indented two spaces:
5669
5770<table>
58−<caption>Demonstration of simple table syntax.</caption><tr>
71+<caption>Demonstration of simple table syntax.</caption>
72+<thead>
73+<tr class="header">
5974<th align="right">Right</th>
6075<th align="left">Left</th>
6176<th align="center">Center</th>
6277<th align="left">Default</th>
63−</tr><tr>
78+</tr>
79+</thead>
80+<tbody>
81+<tr class="odd">
6482<td align="right">12</td>
6583<td align="left">12</td>
6684<td align="center">12</td>
6785<td align="left">12</td>
6886</tr>
69−<tr>
87+<tr class="even">
7088<td align="right">123</td>
7189<td align="left">123</td>
7290<td align="center">123</td>
7391<td align="left">123</td>
7492</tr>
75−<tr>
93+<tr class="odd">
7694<td align="right">1</td>
7795<td align="left">1</td>
7896<td align="center">1</td>
7997<td align="left">1</td>
8098</tr>
99+</tbody>
81100</table>
101+
82102Multiline table with caption:
83103
84104<table>
85−<caption>Here's the caption. It may span multiple lines.</caption><tr>
86−<th align="center" style="width: 15%;">Centered Header</th>
87−<th align="left" style="width: 13%;">Left Aligned</th>
88−<th align="right" style="width: 16%;">Right Aligned</th>
89−<th align="left" style="width: 33%;">Default aligned</th>
90−</tr><tr>
105+<caption>Here's the caption. It may span multiple lines.</caption>
106+<col width="15%" />
107+<col width="13%" />
108+<col width="16%" />
109+<col width="33%" />
110+<thead>
111+<tr class="header">
112+<th align="center">Centered Header</th>
113+<th align="left">Left Aligned</th>
114+<th align="right">Right Aligned</th>
115+<th align="left">Default aligned</th>
116+</tr>
117+</thead>
118+<tbody>
119+<tr class="odd">
91120<td align="center">First</td>
92121<td align="left">row</td>
93122<td align="right">12.0</td>
94123<td align="left">Example of a row that spans multiple lines.</td>
95124</tr>
96−<tr>
125+<tr class="even">
97126<td align="center">Second</td>
98127<td align="left">row</td>
99128<td align="right">5.0</td>
100129<td align="left">Here's another one. Note the blank line between rows.</td>
101130</tr>
131+</tbody>
102132</table>
133+
103134Multiline table without caption:
104135
105136<table>
106−<tr>
107−<th align="center" style="width: 15%;">Centered Header</th>
108−<th align="left" style="width: 13%;">Left Aligned</th>
109−<th align="right" style="width: 16%;">Right Aligned</th>
110−<th align="left" style="width: 33%;">Default aligned</th>
111−</tr><tr>
137+<col width="15%" />
138+<col width="13%" />
139+<col width="16%" />
140+<col width="33%" />
141+<thead>
142+<tr class="header">
143+<th align="center">Centered Header</th>
144+<th align="left">Left Aligned</th>
145+<th align="right">Right Aligned</th>
146+<th align="left">Default aligned</th>
147+</tr>
148+</thead>
149+<tbody>
150+<tr class="odd">
112151<td align="center">First</td>
113152<td align="left">row</td>
114153<td align="right">12.0</td>
115154<td align="left">Example of a row that spans multiple lines.</td>
116155</tr>
117−<tr>
156+<tr class="even">
118157<td align="center">Second</td>
119158<td align="left">row</td>
120159<td align="right">5.0</td>
121160<td align="left">Here's another one. Note the blank line between rows.</td>
122161</tr>
162+</tbody>
123163</table>
164+
165+Table without column headers:
166+
167+<table>
168+<tbody>
169+<tr class="odd">
170+<td align="right">12</td>
171+<td align="left">12</td>
172+<td align="center">12</td>
173+<td align="right">12</td>
174+</tr>
175+<tr class="even">
176+<td align="right">123</td>
177+<td align="left">123</td>
178+<td align="center">123</td>
179+<td align="right">123</td>
180+</tr>
181+<tr class="odd">
182+<td align="right">1</td>
183+<td align="left">1</td>
184+<td align="center">1</td>
185+<td align="right">1</td>
186+</tr>
187+</tbody>
188+</table>
189+
190+Multiline table without column headers:
191+
192+<table>
193+<col width="15%" />
194+<col width="13%" />
195+<col width="16%" />
196+<col width="33%" />
197+<tbody>
198+<tr class="odd">
199+<td align="center">First</td>
200+<td align="left">row</td>
201+<td align="right">12.0</td>
202+<td align="left">Example of a row that spans multiple lines.</td>
203+</tr>
204+<tr class="even">
205+<td align="center">Second</td>
206+<td align="left">row</td>
207+<td align="right">5.0</td>
208+<td align="left">Here's another one. Note the blank line between rows.</td>
209+</tr>
210+</tbody>
211+</table>
212+