From fc1c17b174eabf10f11bf45c4762569cce505956 Mon Sep 17 00:00:00 2001
From: John MacFarlane <fiddlosopher@gmail.com>
Date: Wed, 16 Nov 2011 14:52:10 -0800
Subject: Added an asciidoc writer (partial).

Still TODO:

- documentation in README
- add default.asciidoc to templates/
- lists
- tables
- proper escaping
- footnotes with blank lines - print separately at end?
  currently they are just ignored.
- fix header (date gives weird result on pandoc README)
---
 tests/tables.asciidoc | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 tests/tables.asciidoc

(limited to 'tests/tables.asciidoc')

diff --git a/tests/tables.asciidoc b/tests/tables.asciidoc
new file mode 100644
index 000000000..48cdce852
--- /dev/null
+++ b/tests/tables.asciidoc
@@ -0,0 +1 @@
+placeholder
-- 
cgit v1.2.3


From f6a0e75389d748818e703566b749af293195b4ee Mon Sep 17 00:00:00 2001
From: John MacFarlane <fiddlosopher@gmail.com>
Date: Fri, 18 Nov 2011 09:50:32 -0800
Subject: Supported tables in asciidoc, added table tests.

---
 src/Text/Pandoc/Writers/Asciidoc.hs | 92 +++++++++++++++++++++----------------
 tests/tables.asciidoc               | 72 ++++++++++++++++++++++++++++-
 2 files changed, 124 insertions(+), 40 deletions(-)

(limited to 'tests/tables.asciidoc')

diff --git a/src/Text/Pandoc/Writers/Asciidoc.hs b/src/Text/Pandoc/Writers/Asciidoc.hs
index aaa6964f1..28c99b12a 100644
--- a/src/Text/Pandoc/Writers/Asciidoc.hs
+++ b/src/Text/Pandoc/Writers/Asciidoc.hs
@@ -36,7 +36,7 @@ import Text.Pandoc.Templates (renderTemplate)
 import Text.Pandoc.Shared
 import Text.Pandoc.Parsing hiding (blankline)
 import Text.ParserCombinators.Parsec ( runParser, GenParser )
-import Data.List ( isPrefixOf, intersperse, intercalate, transpose )
+import Data.List ( isPrefixOf, intercalate )
 import Text.Pandoc.Pretty
 import Control.Monad.State
 
@@ -142,44 +142,58 @@ blockToAsciidoc opts (Table caption aligns widths headers rows) =  do
   caption' <- inlineListToAsciidoc opts caption
   let caption'' = if null caption
                      then empty
-                     else blankline <> ": " <> caption' <> blankline
-  headers' <- mapM (blockListToAsciidoc opts) headers
-  let alignHeader alignment = case alignment of
-                                AlignLeft    -> lblock
-                                AlignCenter  -> cblock
-                                AlignRight   -> rblock
-                                AlignDefault -> lblock
-  rawRows <- mapM (mapM (blockListToAsciidoc opts)) rows
-  let isSimple = all (==0) widths
-  let numChars = maximum . map offset
-  let widthsInChars =
-       if isSimple
-          then map ((+2) . numChars) $ transpose (headers' : rawRows)
-          else map (floor . (fromIntegral (writerColumns opts) *)) widths
-  let makeRow = hcat . intersperse (lblock 1 (text " ")) .
-                   (zipWith3 alignHeader aligns widthsInChars)
-  let rows' = map makeRow rawRows
-  let head' = makeRow headers'
-  let maxRowHeight = maximum $ map height (head':rows')
-  let underline = cat $ intersperse (text " ") $
-                  map (\width -> text (replicate width '-')) widthsInChars
-  let border = if maxRowHeight > 1
-                  then text (replicate (sum widthsInChars +
-                          length widthsInChars - 1) '-')
-                  else if all null headers
-                          then underline
-                          else empty
-  let head'' = if all null headers
-                  then empty
-                  else border <> cr <> head'
-  let body = if maxRowHeight > 1
-                then vsep rows'
-                else vcat rows'
-  let bottom = if all null headers
-                  then underline
-                  else border
-  return $ nest 2 $ head'' $$ underline $$ body $$
-              bottom $$ blankline $$ caption'' $$ blankline
+                     else "." <> caption' <> cr
+  let isSimple = all (== 0) widths
+  let relativePercentWidths = if isSimple
+                                 then widths
+                                 else map (/ (sum widths)) widths
+  let widths'' :: [Integer]
+      widths'' = map (floor . (* 100)) relativePercentWidths
+  -- ensure that the widths sum to 100
+  let widths' = case widths'' of
+                     _ | isSimple -> widths''
+                     (w:ws) | sum (w:ws) < 100
+                               -> (100 - sum ws) : ws
+                     ws        -> ws
+  let totalwidth :: Integer
+      totalwidth = floor $ sum widths * 100
+  let colspec al wi = (case al of
+                         AlignLeft    -> "<"
+                         AlignCenter  -> "^"
+                         AlignRight   -> ">"
+                         AlignDefault -> "") ++
+                      if wi == 0 then "" else (show wi ++ "%")
+  let headerspec = if all null headers
+                      then empty
+                      else text "options=\"header\","
+  let widthspec = if totalwidth == 0
+                     then empty
+                     else text "width="
+                          <> doubleQuotes (text $ show totalwidth ++ "%")
+                          <> text ","
+  let tablespec = text "["
+         <> widthspec
+         <> text "cols="
+         <> doubleQuotes (text $ intercalate ","
+             $ zipWith colspec aligns widths')
+         <> text ","
+         <> headerspec <> text "]"
+  let makeCell [Plain x] = do d <- blockListToAsciidoc opts [Plain x]
+                              return $ text "|" <> chomp d
+      makeCell [Para x]  = makeCell [Plain x]
+      makeCell _         = return $ text "|" <> "[multiblock cell omitted]"
+  let makeRow cells = hsep `fmap` mapM makeCell cells
+  rows' <- mapM makeRow rows
+  head' <- makeRow headers
+  let head'' = if all null headers then empty else head'
+  let colwidth = if writerWrapText opts
+                    then writerColumns opts
+                    else 100000
+  let maxwidth = maximum $ map offset (head':rows')
+  let body = if maxwidth > colwidth then vsep rows' else vcat rows'
+  let border = text $ "|" ++ replicate ((min maxwidth colwidth) - 1) '='
+  return $
+    caption'' $$ tablespec $$ border $$ head'' $$ body $$ border $$ blankline
 blockToAsciidoc opts (BulletList items) = do
   contents <- mapM (bulletListItemToAsciidoc opts) items
   return $ cat contents <> blankline
diff --git a/tests/tables.asciidoc b/tests/tables.asciidoc
index 48cdce852..842a9b8c4 100644
--- a/tests/tables.asciidoc
+++ b/tests/tables.asciidoc
@@ -1 +1,71 @@
-placeholder
+Simple table with caption:
+
+.Demonstration of simple table syntax.
+[cols=">,<,^,",options="header",]
+|============================
+|Right |Left |Center |Default
+|12 |12 |12 |12
+|123 |123 |123 |123
+|1 |1 |1 |1
+|============================
+
+Simple table without caption:
+
+[cols=">,<,^,",options="header",]
+|============================
+|Right |Left |Center |Default
+|12 |12 |12 |12
+|123 |123 |123 |123
+|1 |1 |1 |1
+|============================
+
+Simple table indented two spaces:
+
+.Demonstration of simple table syntax.
+[cols=">,<,^,",options="header",]
+|============================
+|Right |Left |Center |Default
+|12 |12 |12 |12
+|123 |123 |123 |123
+|1 |1 |1 |1
+|============================
+
+Multiline table with caption:
+
+.Here's the caption. It may span multiple lines.
+[width="78%",cols="^21%,<17%,>20%,<42%",options="header",]
+|=======================================================================
+|Centered Header |Left Aligned |Right Aligned |Default aligned
+|First |row |12.0 |Example of a row that spans multiple lines.
+|Second |row |5.0 |Here's another one. Note the blank line between rows.
+|=======================================================================
+
+Multiline table without caption:
+
+[width="78%",cols="^21%,<17%,>20%,<42%",options="header",]
+|=======================================================================
+|Centered Header |Left Aligned |Right Aligned |Default aligned
+|First |row |12.0 |Example of a row that spans multiple lines.
+|Second |row |5.0 |Here's another one. Note the blank line between rows.
+|=======================================================================
+
+Table without column headers:
+
+[cols=">,<,^,>",]
+|=======================================================================
+|12 |12 |12 |12
+
+|123 |123 |123 |123
+
+|1 |1 |1 |1
+|=======================================================================
+
+Multiline table without column headers:
+
+[width="78%",cols="^21%,<17%,>20%,42%",]
+|=======================================================================
+|First |row |12.0 |Example of a row that spans multiple lines.
+
+|Second |row |5.0 |Here's another one. Note the blank line between rows.
+|=======================================================================
+
-- 
cgit v1.2.3


From 679e94e53d36efa24507cae960533a6a0f9053e8 Mon Sep 17 00:00:00 2001
From: John MacFarlane <fiddlosopher@gmail.com>
Date: Fri, 18 Nov 2011 19:50:45 -0800
Subject: Updated tests for correct col width.

---
 tests/tables.asciidoc |  8 ++++----
 tests/writer.asciidoc | 29 ++++++++++++++---------------
 2 files changed, 18 insertions(+), 19 deletions(-)

(limited to 'tests/tables.asciidoc')

diff --git a/tests/tables.asciidoc b/tests/tables.asciidoc
index 842a9b8c4..38daca192 100644
--- a/tests/tables.asciidoc
+++ b/tests/tables.asciidoc
@@ -52,20 +52,20 @@ Multiline table without caption:
 Table without column headers:
 
 [cols=">,<,^,>",]
-|=======================================================================
+|=============================================================================
 |12 |12 |12 |12
 
 |123 |123 |123 |123
 
 |1 |1 |1 |1
-|=======================================================================
+|=============================================================================
 
 Multiline table without column headers:
 
 [width="78%",cols="^21%,<17%,>20%,42%",]
-|=======================================================================
+|=============================================================================
 |First |row |12.0 |Example of a row that spans multiple lines.
 
 |Second |row |5.0 |Here's another one. Note the blank line between rows.
-|=======================================================================
+|=============================================================================
 
diff --git a/tests/writer.asciidoc b/tests/writer.asciidoc
index 514c72976..af27e02ce 100644
--- a/tests/writer.asciidoc
+++ b/tests/writer.asciidoc
@@ -4,8 +4,8 @@ Pandoc Test Suite
 :author: Anonymous
 :date: July 17, 2006
 
-This is a set of tests for pandoc. Most of them are adapted from John
-Gruber’s markdown test suite.
+This is a set of tests for pandoc. Most of them are adapted from John Gruber’s
+markdown test suite.
 
 '''''
 
@@ -46,9 +46,9 @@ Paragraphs
 
 Here’s a regular paragraph.
 
-In Markdown 1.0.0 and earlier. Version 8. This line turns into a list
-item. Because a hard-wrapped line in the middle of a paragraph looked
-like a list item.
+In Markdown 1.0.0 and earlier. Version 8. This line turns into a list item.
+Because a hard-wrapped line in the middle of a paragraph looked like a list
+item.
 
 Here’s one with a bullet. \* criminey.
 
@@ -446,8 +446,8 @@ Smart quotes, ellipses, dashes
 
 `He said, ``I want to go.''' Were you alive in the 70’s?
 
-Here is some quoted ``code`' and a
-``http://example.com/?foo=1&bar=2[quoted link]''.
+Here is some quoted ``code`' and a ``http://example.com/?foo=1&bar=2[quoted
+link]''.
 
 Some dashes: one—two — three—four — five.
 
@@ -594,8 +594,7 @@ Foo link:/url/[biz].
 With ampersands
 ~~~~~~~~~~~~~~~
 
-Here’s a http://example.com/?foo=1&bar=2[link with an ampersand in the
-URL].
+Here’s a http://example.com/?foo=1&bar=2[link with an ampersand in the URL].
 
 Here’s a link with an amersand in the link text: http://att.com/[AT&T].
 
@@ -641,12 +640,12 @@ Footnotes
 ---------
 
 Here is a footnote reference,footnote:[Here is the footnote. It can go
-anywhere after the footnote reference. It need not be placed at the end
-of the document.] and another.[multiblock footnote omitted] This should
-_not_ be a footnote reference, because it contains a space.[\^my note]
-Here is an inline note.footnote:[This is _easier_ to type. Inline notes
-may contain http://google.com[links] and `]` verbatim characters, as
-well as [bracketed text].]
+anywhere after the footnote reference. It need not be placed at the end of the
+document.] and another.[multiblock footnote omitted] This should _not_ be a
+footnote reference, because it contains a space.[\^my note] Here is an inline
+note.footnote:[This is _easier_ to type. Inline notes may contain
+http://google.com[links] and `]` verbatim characters, as well as [bracketed
+text].]
 
 ___________________________________________
 Notes can go in quotes.footnote:[In quote.]
-- 
cgit v1.2.3

