git.iko.soy/typeable/pandoc
on master help sign in
Added Text.Pandoc.Writers.OPML.

TODO: * Document in README * Add tests * Add template (and add template to cabal file)

commit69acb47a3410b7b75e27e1abe1f9add3dbe09820 authorJohn MacFarlane <fiddlosopher@gmail.com> dateTue 19 Mar 2013, 20:35 (14 years ago) parent8aa6172
5 files changed+95−2
pandoc.cabal+2−1 pandoc.hs+1 src/Text/Pandoc.hs+3 src/Text/Pandoc/Writers/OPML.hs+81 src/Text/Pandoc/XML.hs+8−1
pandoc.cabal+2−1
@@ -18,7 +18,7 @@ Description: Pandoc is a Haskell library for converting from one markup
1818 this library. It can read markdown and (subsets of) HTML,
1919 reStructuredText, LaTeX, DocBook, MediaWiki markup, OPML,
2020 and Textile, and it can write markdown, reStructuredText,
21− HTML, LaTeX, ConTeXt, Docbook, OpenDocument, ODT,
21+ HTML, LaTeX, ConTeXt, Docbook, OPML, OpenDocument, ODT,
2222 Word docx, RTF, MediaWiki, Textile, groff man pages,
2323 plain text, Emacs Org-Mode, AsciiDoc, EPUB (v2 and v3),
2424 FictionBook2, and S5, Slidy and Slideous HTML slide shows.
@@ -286,6 +286,7 @@ Library
286286 Text.Pandoc.Readers.Native,
287287 Text.Pandoc.Writers.Native,
288288 Text.Pandoc.Writers.Docbook,
289+ Text.Pandoc.Writers.OPML,
289290 Text.Pandoc.Writers.HTML,
290291 Text.Pandoc.Writers.LaTeX,
291292 Text.Pandoc.Writers.ConTeXt,
pandoc.hs+1
@@ -802,6 +802,7 @@ defaultWriterName x =
802802 ".asciidoc" -> "asciidoc"
803803 ".pdf" -> "latex"
804804 ".fb2" -> "fb2"
805+ ".opml" -> "opml"
805806 ['.',y] | y `elem` ['1'..'9'] -> "man"
806807 _ -> "html"
807808
src/Text/Pandoc.hs+3
@@ -86,6 +86,7 @@ module Text.Pandoc
8686 , writeHtml
8787 , writeHtmlString
8888 , writeDocbook
89+ , writeOPML
8990 , writeOpenDocument
9091 , writeMan
9192 , writeMediaWiki
@@ -131,6 +132,7 @@ import Text.Pandoc.Writers.Docx
131132import Text.Pandoc.Writers.EPUB
132133import Text.Pandoc.Writers.FB2
133134import Text.Pandoc.Writers.Docbook
135+import Text.Pandoc.Writers.OPML
134136import Text.Pandoc.Writers.OpenDocument
135137import Text.Pandoc.Writers.Man
136138import Text.Pandoc.Writers.RTF
@@ -230,6 +232,7 @@ writers = [
230232 writeHtmlString o{ writerSlideVariant = DZSlides
231233 , writerHtml5 = True })
232234 ,("docbook" , PureStringWriter writeDocbook)
235+ ,("opml" , PureStringWriter writeOPML)
233236 ,("opendocument" , PureStringWriter writeOpenDocument)
234237 ,("latex" , PureStringWriter writeLaTeX)
235238 ,("beamer" , PureStringWriter $ \o ->
src/Text/Pandoc/Writers/OPML.hsnew file+81
@@ -0,0 +1,81 @@
1+{-
2+Copyright (C) 2013 John MacFarlane <jgm@berkeley.edu>
3+
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.
8+
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.
13+
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
17+-}
18+
19+{- |
20+ Module : Text.Pandoc.Writers.OPML
21+ Copyright : Copyright (C) 2013 John MacFarlane
22+ License : GNU GPL, version 2 or above
23+
24+ Maintainer : John MacFarlane <jgm@berkeley.edu>
25+ Stability : alpha
26+ Portability : portable
27+
28+Conversion of 'Pandoc' documents to OPML XML.
29+-}
30+module Text.Pandoc.Writers.OPML ( writeOPML) where
31+import Text.Pandoc.Definition
32+import Text.Pandoc.XML
33+import Text.Pandoc.Shared
34+import Text.Pandoc.Options
35+import Text.Pandoc.Templates (renderTemplate)
36+import Text.Pandoc.Writers.HTML (writeHtmlString)
37+import Text.Pandoc.Writers.Markdown (writeMarkdown)
38+import Data.List ( intercalate )
39+import Text.Pandoc.Pretty
40+
41+-- | Convert Pandoc document to string in OPML format.
42+writeOPML :: WriterOptions -> Pandoc -> String
43+writeOPML opts (Pandoc (Meta tit auths dat) blocks) =
44+ let title = writeHtmlInlines tit
45+ author = writeHtmlInlines $ intercalate [Space,Str ";",Space] auths
46+ date = trim $ writeHtmlInlines dat
47+ elements = hierarchicalize blocks
48+ colwidth = if writerWrapText opts
49+ then Just $ writerColumns opts
50+ else Nothing
51+ render' = render colwidth
52+ main = render' $ vcat (map (elementToOPML opts) elements)
53+ context = writerVariables opts ++
54+ [ ("body", main)
55+ , ("title", title)
56+ , ("date", date)
57+ , ("author", author) ]
58+ in if writerStandalone opts
59+ then renderTemplate context $ writerTemplate opts
60+ else main
61+
62+writeHtmlInlines :: [Inline] -> String
63+writeHtmlInlines ils = trim $ writeHtmlString def
64+ $ Pandoc (Meta [] [] []) [Plain ils]
65+
66+-- | Convert an Element to OPML.
67+elementToOPML :: WriterOptions -> Element -> Doc
68+elementToOPML _ (Blk _) = empty
69+elementToOPML opts (Sec _ _num _ title elements) =
70+ let isBlk (Blk _) = True
71+ isBlk _ = False
72+ fromBlk (Blk x) = x
73+ fromBlk _ = error "fromBlk called on non-block"
74+ (blocks, rest) = span isBlk elements
75+ attrs = [("text", writeHtmlInlines title)] ++
76+ [("_note", writeMarkdown opts (Pandoc (Meta [] [] [])
77+ (map fromBlk blocks)))
78+ | not (null blocks)]
79+ in inTags True "outline" attrs $
80+ vcat (map (elementToOPML opts) rest)
81+
src/Text/Pandoc/XML.hs+8−1
@@ -64,11 +64,18 @@ escapeCharForXML x = case x of
6464escapeStringForXML :: String -> String
6565escapeStringForXML = concatMap escapeCharForXML
6666
67+-- | Escape newline characters as &#10;
68+escapeNls :: String -> String
69+escapeNls (x:xs)
70+ | x == '\n' = "&#10;" ++ escapeNls xs
71+ | otherwise = x : escapeNls xs
72+escapeNls [] = []
73+
6774-- | Return a text object with a string of formatted XML attributes.
6875attributeList :: [(String, String)] -> Doc
6976attributeList = hcat . map
7077 (\(a, b) -> text (' ' : escapeStringForXML a ++ "=\"" ++
71− escapeStringForXML b ++ "\""))
78+ escapeNls (escapeStringForXML b) ++ "\""))
7279
7380-- | Put the supplied contents between start and end tags of tagType,
7481-- with specified attributes and (if specified) indentation.