@@ -0,0 +1,413 @@
2+Copyright (C) 2010 John MacFarlane <jgm@berkeley.edu>
4+This program is free software; you can redistribute it and/or modify
5+it under the terms of the GNU General Public License as published by
6+the Free Software Foundation; either version 2 of the License, or
7+(at your option) any later version.
9+This program is distributed in the hope that it will be useful,
10+but WITHOUT ANY WARRANTY; without even the implied warranty of
11+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+GNU General Public License for more details.
14+You should have received a copy of the GNU General Public License
15+along with this program; if not, write to the Free Software
16+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20+ Module : Text.Pandoc.Writers.Textile
21+ Copyright : Copyright (C) 2010 John MacFarlane
22+ License : GNU GPL, version 2 or above
24+ Maintainer : John MacFarlane <jgm@berkeley.edu>
26+ Portability : portable
28+Conversion of 'Pandoc' documents to Textile markup.
30+Textile: <http://thresholdstate.com/articles/4312/the-textile-reference-manual>
32+module Text.Pandoc.Writers.Textile ( writeTextile ) where
33+import Text.Pandoc.Definition
34+import Text.Pandoc.Shared
35+import Text.Pandoc.Templates (renderTemplate)
36+import Text.Pandoc.XML ( escapeStringForXML )
37+import Data.List ( intercalate )
38+import Control.Monad.State
39+import Data.Char ( isSpace )
41+data WriterState = WriterState {
42+ stNotes :: [String] -- Footnotes
43+ , stListLevel :: [Char] -- String at beginning of list items, e.g. "**"
44+ , stUseTags :: Bool -- True if we should use HTML tags because we're in a complex list
47+-- | Convert Pandoc to Textile.
48+writeTextile :: WriterOptions -> Pandoc -> String
49+writeTextile opts document =
50+ evalState (pandocToTextile opts document)
51+ (WriterState { stNotes = [], stListLevel = [], stUseTags = False })
53+-- | Return Textile representation of document.
54+pandocToTextile :: WriterOptions -> Pandoc -> State WriterState String
55+pandocToTextile opts (Pandoc _ blocks) = do
56+ body <- blockListToTextile opts blocks
57+ notes <- liftM (unlines . reverse . stNotes) get
58+ let main = body ++ if null notes then "" else ("\n\n" ++ notes)
59+ let context = writerVariables opts ++ [ ("body", main) ]
60+ if writerStandalone opts
61+ then return $ renderTemplate context $ writerTemplate opts
64+withUseTags :: State WriterState a -> State WriterState a
65+withUseTags action = do
66+ oldUseTags <- liftM stUseTags get
67+ modify $ \s -> s { stUseTags = True }
69+ modify $ \s -> s { stUseTags = oldUseTags }
72+-- | Escape one character as needed for Textile.
73+escapeCharForTextile :: Char -> String
74+escapeCharForTextile x = case x of
85+-- | Escape string as needed for Textile.
86+escapeStringForTextile :: String -> String
87+escapeStringForTextile = concatMap escapeCharForTextile
89+-- | Convert Pandoc block element to Textile.
90+blockToTextile :: WriterOptions -- ^ Options
91+ -> Block -- ^ Block element
92+ -> State WriterState String
94+blockToTextile _ Null = return ""
96+blockToTextile opts (Plain inlines) =
97+ inlineListToTextile opts inlines
99+blockToTextile opts (Para [Image txt (src,tit)]) = do
100+ capt <- blockToTextile opts (Para txt)
101+ im <- inlineToTextile opts (Image txt (src,tit))
102+ return $ im ++ "\n" ++ capt
104+blockToTextile opts (Para inlines) = do
105+ useTags <- liftM stUseTags get
106+ listLevel <- liftM stListLevel get
107+ contents <- inlineListToTextile opts inlines
109+ then " <p>" ++ contents ++ "</p>"
110+ else contents ++ if null listLevel then "\n" else ""
112+blockToTextile _ (RawHtml str) = return str
114+blockToTextile _ HorizontalRule = return "<hr />\n"
116+blockToTextile opts (Header level inlines) = do
117+ contents <- inlineListToTextile opts inlines
118+ let prefix = 'h' : (show level ++ ". ")
119+ return $ prefix ++ contents ++ "\n"
121+blockToTextile _ (CodeBlock (_,classes,_) str) =
122+ return $ "bc" ++ classes' ++ dots ++ escapeStringForXML str ++ "\n"
123+ where classes' = if null classes
125+ else "(" ++ unwords classes ++ ")"
126+ dots = if any isBlank (lines str)
129+ isBlank = all isSpace
131+blockToTextile opts (BlockQuote bs@[Para _]) = do
132+ contents <- blockListToTextile opts bs
133+ return $ "bq. " ++ contents
135+blockToTextile opts (BlockQuote blocks) = do
136+ contents <- blockListToTextile opts blocks
137+ return $ "<blockquote>\n\n" ++ contents ++ "\n</blockquote>\n"
139+blockToTextile opts (Table [] aligns widths headers rows') |
140+ all (==0) widths && all (`elem` [AlignLeft,AlignDefault]) aligns = do
141+ hs <- mapM (liftM (("_. " ++) . stripTrailingNewlines) . blockListToTextile opts) headers
142+ let cellsToRow cells = "|" ++ intercalate "|" cells ++ "|"
143+ let header = if all null headers then "" else cellsToRow hs
144+ let rowToCells = mapM (liftM stripTrailingNewlines . blockListToTextile opts)
145+ bs <- mapM rowToCells rows'
146+ let body = unlines $ map cellsToRow bs
147+ return $ header ++ "\n" ++ body ++ "\n"
149+blockToTextile opts (Table capt aligns widths headers rows') = do
150+ let alignStrings = map alignmentToString aligns
151+ captionDoc <- if null capt
154+ c <- inlineListToTextile opts capt
155+ return $ " <caption>" ++ c ++ "</caption>\n"
156+ let percent w = show (truncate (100*w) :: Integer) ++ "%"
157+ let coltags = if all (== 0.0) widths
160+ (\w -> " <col width=\"" ++ percent w ++ "\" />") widths
161+ head' <- if all null headers
164+ hs <- tableRowToTextile opts alignStrings 0 headers
165+ return $ " <thead>\n" ++ hs ++ "\n </thead>\n"
166+ body' <- zipWithM (tableRowToTextile opts alignStrings) [1..] rows'
167+ return $ " <table>\n" ++ captionDoc ++ coltags ++ head' ++
168+ " <tbody>\n" ++ unlines body' ++ " </tbody>\n </table>\n"
170+blockToTextile opts x@(BulletList items) = do
171+ oldUseTags <- liftM stUseTags get
172+ let useTags = oldUseTags || not (isSimpleList x)
175+ contents <- withUseTags $ mapM (listItemToTextile opts) items
176+ return $ " <ul>\n" ++ vcat contents ++ " </ul>\n"
178+ modify $ \s -> s { stListLevel = stListLevel s ++ "*" }
179+ contents <- mapM (listItemToTextile opts) items
180+ modify $ \s -> s { stListLevel = init (stListLevel s) }
181+ return $ vcat contents ++ "\n"
183+blockToTextile opts x@(OrderedList attribs items) = do
184+ oldUseTags <- liftM stUseTags get
185+ let useTags = oldUseTags || not (isSimpleList x)
188+ contents <- withUseTags $ mapM (listItemToTextile opts) items
189+ return $ " <ol" ++ listAttribsToString attribs ++ ">\n" ++ vcat contents ++
192+ modify $ \s -> s { stListLevel = stListLevel s ++ "#" }
193+ contents <- mapM (listItemToTextile opts) items
194+ modify $ \s -> s { stListLevel = init (stListLevel s) }
195+ return $ vcat contents ++ "\n"
197+blockToTextile opts (DefinitionList items) = do
198+ contents <- withUseTags $ mapM (definitionListItemToTextile opts) items
199+ return $ " <dl>\n" ++ vcat contents ++ " </dl>\n"
201+-- Auxiliary functions for lists:
203+-- | Convert ordered list attributes to HTML attribute string
204+listAttribsToString :: ListAttributes -> String
205+listAttribsToString (startnum, numstyle, _) =
206+ let numstyle' = camelCaseToHyphenated $ show numstyle
207+ in (if startnum /= 1
208+ then " start=\"" ++ show startnum ++ "\""
210+ (if numstyle /= DefaultStyle
211+ then " style=\"list-style-type: " ++ numstyle' ++ ";\""
214+-- | Convert bullet or ordered list item (list of blocks) to Textile.
215+listItemToTextile :: WriterOptions -> [Block] -> State WriterState String
216+listItemToTextile opts items = do
217+ contents <- blockListToTextile opts items
218+ useTags <- get >>= return . stUseTags
220+ then return $ " <li>" ++ contents ++ "</li>"
222+ marker <- get >>= return . stListLevel
223+ return $ marker ++ " " ++ contents
225+-- | Convert definition list item (label, list of blocks) to Textile.
226+definitionListItemToTextile :: WriterOptions
227+ -> ([Inline],[[Block]])
228+ -> State WriterState String
229+definitionListItemToTextile opts (label, items) = do
230+ labelText <- inlineListToTextile opts label
231+ contents <- mapM (blockListToTextile opts) items
232+ return $ " <dt>" ++ labelText ++ "</dt>\n" ++
233+ (intercalate "\n" $ map (\d -> " <dd>" ++ d ++ "</dd>") contents)
235+-- | True if the list can be handled by simple wiki markup, False if HTML tags will be needed.
236+isSimpleList :: Block -> Bool
239+ BulletList items -> all isSimpleListItem items
240+ OrderedList (num, sty, _) items -> all isSimpleListItem items &&
241+ num == 1 && sty `elem` [DefaultStyle, Decimal]
244+-- | True if list item can be handled with the simple wiki syntax. False if
245+-- HTML tags will be needed.
246+isSimpleListItem :: [Block] -> Bool
247+isSimpleListItem [] = True
248+isSimpleListItem [x] =
252+ BulletList _ -> isSimpleList x
253+ OrderedList _ _ -> isSimpleList x
255+isSimpleListItem [x, y] | isPlainOrPara x =
257+ BulletList _ -> isSimpleList y
258+ OrderedList _ _ -> isSimpleList y
260+isSimpleListItem _ = False
262+isPlainOrPara :: Block -> Bool
263+isPlainOrPara (Plain _) = True
264+isPlainOrPara (Para _) = True
265+isPlainOrPara _ = False
267+-- | Concatenates strings with line breaks between them.
268+vcat :: [String] -> String
269+vcat = intercalate "\n"
271+-- Auxiliary functions for tables. (TODO: these are common to HTML, MediaWiki,
272+-- and Textile writers, and should be abstracted out.)
274+tableRowToTextile :: WriterOptions
278+ -> State WriterState String
279+tableRowToTextile opts alignStrings rownum cols' = do
280+ let celltype = if rownum == 0 then "th" else "td"
281+ let rowclass = case rownum of
283+ x | x `rem` 2 == 1 -> "odd"
285+ cols'' <- sequence $ zipWith
286+ (\alignment item -> tableItemToTextile opts celltype alignment item)
288+ return $ "<tr class=\"" ++ rowclass ++ "\">\n" ++ unlines cols'' ++ "</tr>"
290+alignmentToString :: Alignment -> [Char]
291+alignmentToString alignment = case alignment of
293+ AlignRight -> "right"
294+ AlignCenter -> "center"
295+ AlignDefault -> "left"
297+tableItemToTextile :: WriterOptions
301+ -> State WriterState String
302+tableItemToTextile opts celltype align' item = do
303+ let mkcell x = "<" ++ celltype ++ " align=\"" ++ align' ++ "\">" ++
304+ x ++ "</" ++ celltype ++ ">"
305+ contents <- blockListToTextile opts item
306+ return $ mkcell contents
308+-- | Convert list of Pandoc block elements to Textile.
309+blockListToTextile :: WriterOptions -- ^ Options
310+ -> [Block] -- ^ List of block elements
311+ -> State WriterState String
312+blockListToTextile opts blocks =
313+ mapM (blockToTextile opts) blocks >>= return . vcat
315+-- | Convert list of Pandoc inline elements to Textile.
316+inlineListToTextile :: WriterOptions -> [Inline] -> State WriterState String
317+inlineListToTextile opts lst =
318+ mapM (inlineToTextile opts) lst >>= return . concat
320+-- | Convert Pandoc inline element to Textile.
321+inlineToTextile :: WriterOptions -> Inline -> State WriterState String
323+inlineToTextile opts (Emph lst) = do
324+ contents <- inlineListToTextile opts lst
325+ return $ if '_' `elem` contents
326+ then "<em>" ++ contents ++ "</em>"
327+ else "_" ++ contents ++ "_"
329+inlineToTextile opts (Strong lst) = do
330+ contents <- inlineListToTextile opts lst
331+ return $ if '*' `elem` contents
332+ then "<strong>" ++ contents ++ "</strong>"
333+ else "*" ++ contents ++ "*"
335+inlineToTextile opts (Strikeout lst) = do
336+ contents <- inlineListToTextile opts lst
337+ return $ if '-' `elem` contents
338+ then "<del>" ++ contents ++ "</del>"
339+ else "-" ++ contents ++ "-"
341+inlineToTextile opts (Superscript lst) = do
342+ contents <- inlineListToTextile opts lst
343+ return $ if '^' `elem` contents
344+ then "<sup>" ++ contents ++ "</sup>"
345+ else "^" ++ contents ++ "^"
347+inlineToTextile opts (Subscript lst) = do
348+ contents <- inlineListToTextile opts lst
349+ return $ if '~' `elem` contents
350+ then "<sub>" ++ contents ++ "</sub>"
351+ else "~" ++ contents ++ "~"
353+inlineToTextile opts (SmallCaps lst) = inlineListToTextile opts lst
355+inlineToTextile opts (Quoted SingleQuote lst) = do
356+ contents <- inlineListToTextile opts lst
357+ return $ "'" ++ contents ++ "'"
359+inlineToTextile opts (Quoted DoubleQuote lst) = do
360+ contents <- inlineListToTextile opts lst
361+ return $ "\"" ++ contents ++ "\""
363+inlineToTextile opts (Cite _ lst) = inlineListToTextile opts lst
365+inlineToTextile _ EmDash = return " -- "
367+inlineToTextile _ EnDash = return " - "
369+inlineToTextile _ Apostrophe = return "'"
371+inlineToTextile _ Ellipses = return "..."
373+inlineToTextile _ (Code str) =
374+ return $ if '@' `elem` str
375+ then "<tt>" ++ escapeStringForXML str ++ "</tt>"
376+ else "@" ++ escapeStringForXML str ++ "@"
378+inlineToTextile _ (Str str) = return $ escapeStringForTextile str
380+inlineToTextile _ (Math _ str) =
381+ return $ "<span class=\"math\">" ++ escapeStringForXML str ++ "</math>"
383+inlineToTextile _ (TeX _) = return ""
385+inlineToTextile _ (HtmlInline str) = return str
387+inlineToTextile _ (LineBreak) = return "\n"
389+inlineToTextile _ Space = return " "
391+inlineToTextile opts (Link txt (src, _)) = do
392+ label <- case txt of
393+ [Code s] -> return s
394+ _ -> inlineListToTextile opts txt
395+ return $ "\"" ++ label ++ "\":" ++ src
397+inlineToTextile opts (Image alt (source, tit)) = do
398+ alt' <- inlineListToTextile opts alt
399+ let txt = if null tit
402+ else "(" ++ alt' ++ ")"
403+ else "(" ++ tit ++ ")"
404+ return $ "!" ++ source ++ txt ++ "!"
406+inlineToTextile opts (Note contents) = do
407+ curNotes <- liftM stNotes get
408+ let newnum = length curNotes + 1
409+ contents' <- blockListToTextile opts contents
410+ let thisnote = "fn" ++ show newnum ++ ". " ++ contents' ++ "\n"
411+ modify $ \s -> s { stNotes = thisnote : curNotes }
412+ return $ "[" ++ show newnum ++ "]"
413+ -- note - may not work for notes with multiple blocks