@@ -0,0 +1,118 @@
1+{-
2+Copyright (C) 2015 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.Readers.CommonMark
21+ Copyright : Copyright (C) 2015 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 CommonMark-formatted plain text to 'Pandoc' document.
29+
30+CommonMark is a strongly specified variant of Markdown: http://commonmark.org.
31+-}
32+module Text.Pandoc.Readers.CommonMark (readCommonMark)
33+where
34+
35+import CMark
36+import Data.Text (unpack, pack)
37+import Data.List (groupBy)
38+import Text.Pandoc.Definition
39+import Text.Pandoc.Options
40+
41+-- | Parse a CommonMark formatted string into a 'Pandoc' structure.
42+readCommonMark :: ReaderOptions -> String -> Pandoc
43+readCommonMark opts = nodeToPandoc . commonmarkToNode opts' . pack
44+ where opts' = if readerSmart opts
45+ then [optNormalize, optSmart]
46+ else [optNormalize]
47+
48+nodeToPandoc :: Node -> Pandoc
49+nodeToPandoc (Node _ DOCUMENT nodes) =
50+ Pandoc nullMeta $ foldr addBlock [] nodes
51+nodeToPandoc n = -- shouldn't happen
52+ Pandoc nullMeta $ foldr addBlock [] [n]
53+
54+addBlocks :: [Node] -> [Block]
55+addBlocks = foldr addBlock []
56+
57+addBlock :: Node -> [Block] -> [Block]
58+addBlock (Node _ PARAGRAPH nodes) =
59+ (Para (addInlines nodes) :)
60+addBlock (Node _ HRULE _) =
61+ (HorizontalRule :)
62+addBlock (Node _ BLOCK_QUOTE nodes) =
63+ (BlockQuote (addBlocks nodes) :)
64+addBlock (Node _ (HTML t) _) =
65+ (RawBlock (Format "html") (unpack t) :)
66+addBlock (Node _ (CODE_BLOCK info t) _) =
67+ (CodeBlock ("", take 1 (words (unpack info)), []) (unpack t) :)
68+addBlock (Node _ (HEADER lev) nodes) =
69+ (Header lev ("",[],[]) (addInlines nodes) :)
70+addBlock (Node _ (LIST listAttrs) nodes) =
71+ (constructor (map (setTightness . addBlocks . children) nodes) :)
72+ where constructor = case listType listAttrs of
73+ BULLET_LIST -> BulletList
74+ ORDERED_LIST -> OrderedList
75+ (start, DefaultStyle, delim)
76+ start = listStart listAttrs
77+ setTightness = if listTight listAttrs
78+ then map paraToPlain
79+ else id
80+ paraToPlain (Para xs) = Plain (xs)
81+ paraToPlain x = x
82+ delim = case listDelim listAttrs of
83+ PERIOD_DELIM -> Period
84+ PAREN_DELIM -> OneParen
85+addBlock (Node _ ITEM nodes) = id -- handled in LIST
86+addBlock _ = id
87+
88+children :: Node -> [Node]
89+children (Node _ _ ns) = ns
90+
91+addInlines :: [Node] -> [Inline]
92+addInlines = foldr addInline []
93+
94+addInline :: Node -> [Inline] -> [Inline]
95+addInline (Node _ (TEXT t) _) = (map toinl clumps ++)
96+ where raw = unpack t
97+ clumps = groupBy samekind raw
98+ samekind ' ' ' ' = True
99+ samekind ' ' _ = False
100+ samekind _ ' ' = False
101+ samekind _ _ = True
102+ toinl (' ':_) = Space
103+ toinl xs = Str xs
104+addInline (Node _ LINEBREAK _) = (LineBreak :)
105+addInline (Node _ SOFTBREAK _) = (Space :)
106+addInline (Node _ (INLINE_HTML t) _) =
107+ (RawInline (Format "html") (unpack t) :)
108+addInline (Node _ (CODE t) _) =
109+ (Code ("",[],[]) (unpack t) :)
110+addInline (Node _ EMPH nodes) =
111+ (Emph (addInlines nodes) :)
112+addInline (Node _ STRONG nodes) =
113+ (Strong (addInlines nodes) :)
114+addInline (Node _ (LINK url title) nodes) =
115+ (Link (addInlines nodes) (unpack url, unpack title) :)
116+addInline (Node _ (IMAGE url title) nodes) =
117+ (Image (addInlines nodes) (unpack url, unpack title) :)
118+addInline _ = id