git.iko.soy/typeable/pandoc
on master help sign in
html inlines and html blocks handling in textile reader
d724c6bPaul Rivier
src/Text/Pandoc/Readers/Textile.hs+26−17
@@ -28,7 +28,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28Conversion from Textile to 'Pandoc' document, based on the spec
28Conversion from Textile to 'Pandoc' document, based on the spec
29available at ahttp://redcloth.org/hobix.com/textile/
29available at ahttp://redcloth.org/hobix.com/textile/
30
30
31−Implemented and parsed :
31+Implemented and parsed:
32 - Paragraphs
32 - Paragraphs
33 - Code blocks
33 - Code blocks
34 - Lists
34 - Lists
@@ -36,8 +36,14 @@ Implemented and parsed :
36 - Inlines : strong, emph, cite, code, deleted, superscript,
36 - Inlines : strong, emph, cite, code, deleted, superscript,
37 subscript, links
37 subscript, links
38
38
39−Implemented but discarded :
39+Implemented but discarded:
40 - HTML-specific and CSS-specific attributes
40 - HTML-specific and CSS-specific attributes
41+
42+Left to be implemented:
43+ - Pandoc Meta Information (title, author, date)
44+ - footnotes
45+ - should autolink be shared through Parsing.hs ?
46+
41-}
47-}
42
48
43
49
@@ -47,7 +53,8 @@ module Text.Pandoc.Readers.Textile (
47import Text.Pandoc.Definition
53import Text.Pandoc.Definition
48import Text.Pandoc.Shared
54import Text.Pandoc.Shared
49import Text.Pandoc.Parsing
55import Text.Pandoc.Parsing
50−import Text.Pandoc.Readers.HTML ( htmlTag, htmlEndTag )
56+import Text.Pandoc.Readers.HTML ( htmlTag, htmlEndTag, -- find code blocks
57+ rawHtmlBlock, rawHtmlInline )
51import Text.ParserCombinators.Parsec
58import Text.ParserCombinators.Parsec
52import Data.Char ( digitToInt )
59import Data.Char ( digitToInt )
53
60
@@ -83,6 +90,7 @@ blockParsers = [ codeBlock
83 , header
90 , header
84 , blockQuote
91 , blockQuote
85 , anyList
92 , anyList
93+ , rawHtmlBlock'
86 , maybeExplicitBlock "table" table
94 , maybeExplicitBlock "table" table
87 , maybeExplicitBlock "p" para
95 , maybeExplicitBlock "p" para
88 , nullBlock ]
96 , nullBlock ]
@@ -170,17 +178,26 @@ orderedListItemAtDepth depth = try $ do
170 sublist <- option [] (anyListAtDepth (depth + 1) >>= return . (:[]))
178 sublist <- option [] (anyListAtDepth (depth + 1) >>= return . (:[]))
171 return (p:sublist)
179 return (p:sublist)
172
180
173−-- | This terminates a block such as a paragraph.
181+-- | This terminates a block such as a paragraph. Because of raw html
182+-- blocks support, we have to lookAhead for a rawHtmlBlock.
174blockBreak :: GenParser Char ParserState ()
183blockBreak :: GenParser Char ParserState ()
184+blockBreak = try $ choice
175−blockBreak = try $ newline >> blanklines >> return ()
185+ [newline >> blanklines >> return (),
186+ lookAhead rawHtmlBlock' >> return ()]
176
187
188+-- | A raw Html Block, optionally followed by blanklines
189+rawHtmlBlock' :: GenParser Char ParserState Block
190+rawHtmlBlock' = try $ do
191+ b <- rawHtmlBlock
192+ optional blanklines
193+ return b
177
194
178-- | In textile, paragraphs are separated by blank lines.
195-- | In textile, paragraphs are separated by blank lines.
179para :: GenParser Char ParserState Block
196para :: GenParser Char ParserState Block
180para = try $ do
197para = try $ do
181 content <- manyTill inline blockBreak
198 content <- manyTill inline blockBreak
182 return $ Para $ normalizeSpaces content
199 return $ Para $ normalizeSpaces content
183−
200+
184
201
185-- Tables
202-- Tables
186
203
@@ -260,6 +277,7 @@ inlineParsers = [ autoLink
260 , str
277 , str
261 , whitespace
278 , whitespace
262 , endline
279 , endline
280+ , rawHtmlInline
263 , code
281 , code
264 , simpleInline (string "??") (Cite [])
282 , simpleInline (string "??") (Cite [])
265 , simpleInline (string "**") Strong
283 , simpleInline (string "**") Strong
@@ -313,7 +331,7 @@ image = try $ do
313
331
314-- | Any special symbol defined in specialChars
332-- | Any special symbol defined in specialChars
315symbol :: GenParser Char ParserState Inline
333symbol :: GenParser Char ParserState Inline
316−symbol = do
334+symbol = do
317 result <- oneOf specialChars
335 result <- oneOf specialChars
318 return $ Str [result]
336 return $ Str [result]
319
337
@@ -322,7 +340,6 @@ code :: GenParser Char ParserState Inline
322code = surrounded (char '@') anyChar >>=
340code = surrounded (char '@') anyChar >>=
323 return . Code
341 return . Code
324
342
325−
326-- | Html / CSS attributes
343-- | Html / CSS attributes
327attributes :: GenParser Char ParserState String
344attributes :: GenParser Char ParserState String
328attributes = choice [ enclosed (char '(') (char ')') anyChar,
345attributes = choice [ enclosed (char '(') (char ')') anyChar,
@@ -341,12 +358,4 @@ simpleInline :: GenParser Char ParserState t -- ^ surrounding parser
341 -> GenParser Char ParserState Inline -- ^ content parser (to be used repeatedly)
358 -> GenParser Char ParserState Inline -- ^ content parser (to be used repeatedly)
342simpleInline border construct = surrounded border (inlineWithAttribute) >>=
359simpleInline border construct = surrounded border (inlineWithAttribute) >>=
343 return . construct . normalizeSpaces
360 return . construct . normalizeSpaces
344− where inlineWithAttribute = (try $ optional attributes) >> inline
361+ where inlineWithAttribute = (try $ optional attributes) >> inline
345−
346−
347−-- TODO
348−--
349−-- - Pandoc Meta Information (title, author, date)
350−-- - footnotes
351−-- - should autolink be shared through Parsing.hs ?
352−-- - embeded HTML, both inlines and blocks
tests/textile-reader.native+15−1
@@ -113,4 +113,18 @@ Pandoc (Meta {docTitle = [Str ""], docAuthors = [[Str ""]], docDate = [Str ""]})
113 , [ Plain [Str "sex"] ] ],
113 , [ Plain [Str "sex"] ] ],
114 [ [ Plain [Str "joan"] ]
114 [ [ Plain [Str "joan"] ]
115 , [ Plain [Str "24"] ]
115 , [ Plain [Str "24"] ]
116− , [ Plain [Str "f"] ] ] ] ]
116+ , [ Plain [Str "f"] ] ] ]
117+, Header 1 [Str "Raw",Space,Str "HTML"]
118+, Para [Str "However,",Space,HtmlInline "<strong>",Space,Str "raw",Space,Str "HTML",Space,Str "inlines",Space,HtmlInline "</strong>",Space,Str "are",Space,Str "accepted,",Space,Str "as",Space,Str "well",Space,Str "as",Space,Str ":"]
119+, RawHtml "<div class=\"foobar\">"
120+, Para [Str "any",Space,Strong [Str "Raw",Space,Str "HTML",Space,Str "Block"],Space,Str "with",Space,Str "bold"]
121+, RawHtml "</div>"
122+, Para [Str "Html",Space,Str "blocks",Space,Str "can",Space,Str "be"]
123+, RawHtml "<div>"
124+, Para [Str "inlined"]
125+, RawHtml "</div>"
126+, Para [Str "as",Space,Str "well."]
127+, BulletList
128+ [ [ Plain [Str "this",Space,Str "<",Str "div",Str ">",Space,Str "won",Str "'",Str "t",Space,Str "produce",Space,Str "raw",Space,Str "html",Space,Str "blocks",Space,Str "<",Str "/div",Str ">"] ]
129+ , [ Plain [Str "but",Space,Str "this",Space,HtmlInline "<strong>",Space,Str "will",Space,Str "produce",Space,Str "inline",Space,Str "html",Space,HtmlInline "</strong>"] ] ]
130+, Para [Str "Can",Space,Str "you",Space,Str "prove",Space,Str "that",Space,Str "2",Space,Str "<",Space,Str "3",Space,Str "?"] ]
tests/textile-reader.textile+15
@@ -161,3 +161,18 @@ p{color:green}. and paragraph attributes, and table attributes.
161table{foo:bar}.
161table{foo:bar}.
162| name | age | sex |
162| name | age | sex |
163| joan | 24 | f |
163| joan | 24 | f |
164+
165+h1. Raw HTML
166+
167+However, <strong> raw HTML inlines </strong> are accepted, as well as :
168+
169+<div class="foobar">
170+ any *Raw HTML Block* with bold
171+</div>
172+
173+Html blocks can be <div>inlined</div> as well.
174+
175+* this <div> won't produce raw html blocks </div>
176+* but this <strong> will produce inline html </strong>
177+
178+Can you prove that 2 < 3 ?