diff options
author | fiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> | 2007-01-15 19:52:42 +0000 |
---|---|---|
committer | fiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> | 2007-01-15 19:52:42 +0000 |
commit | 60989d0637780787fb337b94af212f1ee9e1ae22 (patch) | |
tree | 95b5caa1e7e304a47739532a9c4d3767ce54435c /src/Text/Pandoc/Writers | |
parent | 4224d913880e4f77a358cda868c9d1ca75820506 (diff) | |
download | pandoc-60989d0637780787fb337b94af212f1ee9e1ae22.tar.gz |
Added support for tables in markdown reader and in LaTeX,
DocBook, and HTML writers. The syntax is documented in
README. Tests have been added to the test suite.
git-svn-id: https://pandoc.googlecode.com/svn/trunk@493 788f1e2b-df1e-0410-8736-df70ead52e1b
Diffstat (limited to 'src/Text/Pandoc/Writers')
-rw-r--r-- | src/Text/Pandoc/Writers/Docbook.hs | 34 | ||||
-rw-r--r-- | src/Text/Pandoc/Writers/HTML.hs | 32 | ||||
-rw-r--r-- | src/Text/Pandoc/Writers/LaTeX.hs | 33 | ||||
-rw-r--r-- | src/Text/Pandoc/Writers/Markdown.hs | 4 | ||||
-rw-r--r-- | src/Text/Pandoc/Writers/RST.hs | 3 | ||||
-rw-r--r-- | src/Text/Pandoc/Writers/RTF.hs | 2 |
6 files changed, 107 insertions, 1 deletions
diff --git a/src/Text/Pandoc/Writers/Docbook.hs b/src/Text/Pandoc/Writers/Docbook.hs index e67b91fcd..ec3801a9a 100644 --- a/src/Text/Pandoc/Writers/Docbook.hs +++ b/src/Text/Pandoc/Writers/Docbook.hs @@ -151,7 +151,39 @@ blockToDocbook opts (RawHtml str) = text str -- raw XML block blockToDocbook opts HorizontalRule = empty -- not semantic blockToDocbook opts (Note _ _) = empty -- shouldn't occur blockToDocbook opts (Key _ _) = empty -- shouldn't occur -blockToDocbook opts _ = inTagsIndented "para" (text "Unknown block type") +blockToDocbook opts (Table caption aligns widths headers rows) = + let alignStrings = map alignmentToString aligns + captionDoc = if null caption + then empty + else inTagsIndented "caption" + (inlinesToDocbook opts caption) + tableType = if isEmpty captionDoc then "informaltable" else "table" in + inTagsIndented tableType $ captionDoc $$ + (colHeadsToDocbook opts alignStrings widths headers) $$ + (vcat $ map (tableRowToDocbook opts alignStrings) rows) + +colHeadsToDocbook opts alignStrings widths headers = + let heads = zipWith3 + (\align width item -> tableItemToDocbook opts "th" align width item) + alignStrings widths headers in + inTagsIndented "tr" $ vcat heads + +alignmentToString alignment = case alignment of + AlignLeft -> "left" + AlignRight -> "right" + AlignCenter -> "center" + AlignDefault -> "left" + +tableRowToDocbook opts aligns cols = + inTagsIndented "tr" $ vcat $ zipWith3 (tableItemToDocbook opts "td") aligns (repeat 0) cols + +tableItemToDocbook opts tag align width item = + let attrib = [("align", align)] ++ + if (width /= 0) + then [("style", "{width: " ++ + show (truncate (100*width)) ++ "%;}")] + else [] in + inTags True tag attrib $ vcat $ map (blockToDocbook opts) item -- | Put string in CDATA section cdata :: String -> Doc diff --git a/src/Text/Pandoc/Writers/HTML.hs b/src/Text/Pandoc/Writers/HTML.hs index e119a5c87..d38a57556 100644 --- a/src/Text/Pandoc/Writers/HTML.hs +++ b/src/Text/Pandoc/Writers/HTML.hs @@ -186,6 +186,38 @@ blockToHtml opts (Header level lst) = if ((level > 0) && (level <= 6)) then inTagsSimple ("h" ++ show level) contents else inTagsSimple "p" contents +blockToHtml opts (Table caption aligns widths headers rows) = + let alignStrings = map alignmentToString aligns + captionDoc = if null caption + then empty + else inTagsSimple "caption" + (inlineListToHtml opts caption) in + inTagsIndented "table" $ captionDoc $$ + (colHeadsToHtml opts alignStrings widths headers) $$ + (vcat $ map (tableRowToHtml opts alignStrings) rows) + +colHeadsToHtml opts alignStrings widths headers = + let heads = zipWith3 + (\align width item -> tableItemToHtml opts "th" align width item) + alignStrings widths headers in + inTagsIndented "tr" $ vcat heads + +alignmentToString alignment = case alignment of + AlignLeft -> "left" + AlignRight -> "right" + AlignCenter -> "center" + AlignDefault -> "left" + +tableRowToHtml opts aligns cols = + inTagsIndented "tr" $ vcat $ zipWith3 (tableItemToHtml opts "td") aligns (repeat 0) cols + +tableItemToHtml opts tag align width item = + let attrib = [("align", align)] ++ + if (width /= 0) + then [("style", "{width: " ++ + show (truncate (100*width)) ++ "%;}")] + else [] in + inTags False tag attrib $ vcat $ map (blockToHtml opts) item listItemToHtml :: WriterOptions -> [Block] -> Doc listItemToHtml opts list = diff --git a/src/Text/Pandoc/Writers/LaTeX.hs b/src/Text/Pandoc/Writers/LaTeX.hs index aca72535d..db7af223d 100644 --- a/src/Text/Pandoc/Writers/LaTeX.hs +++ b/src/Text/Pandoc/Writers/LaTeX.hs @@ -32,6 +32,7 @@ module Text.Pandoc.Writers.LaTeX ( ) where import Text.Pandoc.Definition import Text.Pandoc.Shared +import Text.Printf ( printf ) import List ( (\\) ) -- | Convert Pandoc to LaTeX. @@ -123,6 +124,38 @@ blockToLaTeX notes (Header level lst) = then "\\" ++ (concat (replicate (level - 1) "sub")) ++ "section{" ++ (inlineListToLaTeX notes (deVerb lst)) ++ "}\n\n" else (inlineListToLaTeX notes lst) ++ "\n\n" +blockToLaTeX notes (Table caption aligns widths heads rows) = + let colWidths = map printDecimal widths + colDescriptors = concat $ zipWith + (\width align -> ">{\\PBS" ++ + (case align of + AlignLeft -> "\\raggedright" + AlignRight -> "\\raggedleft" + AlignCenter -> "\\centering" + AlignDefault -> "\\raggedright") ++ + "\\hspace{0pt}}p{" ++ width ++ + "\\textwidth}") + colWidths aligns + headers = tableRowToLaTeX notes heads + captionText = inlineListToLaTeX notes caption + tableBody = "\\begin{tabular}{" ++ colDescriptors ++ "}\n" ++ + headers ++ "\\hline\n" ++ + (concatMap (tableRowToLaTeX notes) rows) ++ + "\\end{tabular}\n" + centered str = "\\begin{center}\n" ++ str ++ "\\end{center}\n" in + if null captionText + then centered tableBody ++ "\n" + else "\\begin{table}[h]\n" ++ centered tableBody ++ "\\caption{" ++ + captionText ++ "}\n" ++ "\\end{table}\n\n" + + +printDecimal :: Float -> String +printDecimal = printf "%.2f" + +tableColumnWidths notes cols = map (length . (concatMap (blockToLaTeX notes))) cols + +tableRowToLaTeX notes cols = joinWithSep " & " (map (concatMap (blockToLaTeX notes)) cols) ++ "\\\\\n" + listItemToLaTeX notes list = "\\item " ++ (concatMap (blockToLaTeX notes) list) diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs index 343942421..0e7704510 100644 --- a/src/Text/Pandoc/Writers/Markdown.hs +++ b/src/Text/Pandoc/Writers/Markdown.hs @@ -132,6 +132,10 @@ blockToMarkdown tabStop (OrderedList lst) = blockToMarkdown tabStop HorizontalRule = text "\n* * * * *\n" blockToMarkdown tabStop (Header level lst) = text ((replicate level '#') ++ " ") <> (inlineListToMarkdown lst) <> (text "\n") +blockToMarkdown tabStop (Table caption _ _ headers rows) = + blockToMarkdown tabStop (Para [Str "pandoc: TABLE unsupported in Markdown writer"]) + + bulletListItemToMarkdown tabStop list = hang (text "- ") tabStop (vcat (map (blockToMarkdown tabStop) list)) diff --git a/src/Text/Pandoc/Writers/RST.hs b/src/Text/Pandoc/Writers/RST.hs index 7e1581908..b6802ffa2 100644 --- a/src/Text/Pandoc/Writers/RST.hs +++ b/src/Text/Pandoc/Writers/RST.hs @@ -148,6 +148,9 @@ blockToRST tabStop (Header level lst) = let headerChar = if (level > 5) then ' ' else "=-~^'" !! (level - 1) in let border = text $ replicate headerLength headerChar in (headerText <> char '\n' <> border <> char '\n', refs) +blockToRST tabStop (Table caption _ _ headers rows) = + blockToRST tabStop (Para [Str "pandoc: TABLE unsupported in RST writer"]) + -- | Convert bullet list item (list of blocks) to reStructuredText. -- Returns a pair of 'Doc', the first the main text, the second references diff --git a/src/Text/Pandoc/Writers/RTF.hs b/src/Text/Pandoc/Writers/RTF.hs index 20f06d21b..b53e39cb2 100644 --- a/src/Text/Pandoc/Writers/RTF.hs +++ b/src/Text/Pandoc/Writers/RTF.hs @@ -170,6 +170,8 @@ blockToRTF notes indent HorizontalRule = blockToRTF notes indent (Header level lst) = rtfPar indent 0 ("\\b \\fs" ++ (show (40 - (level * 4))) ++ " " ++ (inlineListToRTF notes lst)) +blockToRTF notes indent (Table caption _ _ headers rows) = + blockToRTF notes indent (Para [Str "pandoc: TABLE unsupported in RST writer"]) -- | Ensure that there's the same amount of space after compact -- lists as after regular lists. |