git.iko.soy/typeable/pandoc
on master help sign in
html inlines and html blocks handling in textile reader
commitd724c6b5681b2f5815b7d915d5e39a7d3979e7f8 authorPaul Rivier <paul.r.ml@gmail.com> dateTue 23 Nov 2010, 12:09 (16 years ago) parentfa08668
3 files changed+56−18
src/Text/Pandoc/Readers/Textile.hs+26−17 tests/textile-reader.native+15−1 tests/textile-reader.textile+15
src/Text/Pandoc/Readers/Textile.hs+26−17
@@ -28,7 +28,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2828Conversion from Textile to 'Pandoc' document, based on the spec
2929available at ahttp://redcloth.org/hobix.com/textile/
3030
31−Implemented and parsed :
31+Implemented and parsed:
3232 - Paragraphs
3333 - Code blocks
3434 - Lists
@@ -36,8 +36,14 @@ Implemented and parsed :
3636 - Inlines : strong, emph, cite, code, deleted, superscript,
3737 subscript, links
3838
39−Implemented but discarded :
39+Implemented but discarded:
4040 - 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+
4147-}
4248
4349
@@ -47,7 +53,8 @@ module Text.Pandoc.Readers.Textile (
4753import Text.Pandoc.Definition
4854import Text.Pandoc.Shared
4955import 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 )
5158import Text.ParserCombinators.Parsec
5259import Data.Char ( digitToInt )
5360
@@ -83,6 +90,7 @@ blockParsers = [ codeBlock
8390 , header
8491 , blockQuote
8592 , anyList
93+ , rawHtmlBlock'
8694 , maybeExplicitBlock "table" table
8795 , maybeExplicitBlock "p" para
8896 , nullBlock ]
@@ -170,17 +178,26 @@ orderedListItemAtDepth depth = try $ do
170178 sublist <- option [] (anyListAtDepth (depth + 1) >>= return . (:[]))
171179 return (p:sublist)
172180
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.
174183blockBreak :: GenParser Char ParserState ()
175−blockBreak = try $ newline >> blanklines >> return ()
184+blockBreak = try $ choice
185+ [newline >> blanklines >> return (),
186+ lookAhead rawHtmlBlock' >> return ()]
176187
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
177194
178195-- | In textile, paragraphs are separated by blank lines.
179196para :: GenParser Char ParserState Block
180197para = try $ do
181198 content <- manyTill inline blockBreak
182199 return $ Para $ normalizeSpaces content
183−
200+
184201
185202-- Tables
186203
@@ -260,6 +277,7 @@ inlineParsers = [ autoLink
260277 , str
261278 , whitespace
262279 , endline
280+ , rawHtmlInline
263281 , code
264282 , simpleInline (string "??") (Cite [])
265283 , simpleInline (string "**") Strong
@@ -313,7 +331,7 @@ image = try $ do
313331
314332-- | Any special symbol defined in specialChars
315333symbol :: GenParser Char ParserState Inline
316−symbol = do
334+symbol = do
317335 result <- oneOf specialChars
318336 return $ Str [result]
319337
@@ -322,7 +340,6 @@ code :: GenParser Char ParserState Inline
322340code = surrounded (char '@') anyChar >>=
323341 return . Code
324342
325−
326343-- | Html / CSS attributes
327344attributes :: GenParser Char ParserState String
328345attributes = choice [ enclosed (char '(') (char ')') anyChar,
@@ -341,12 +358,4 @@ simpleInline :: GenParser Char ParserState t -- ^ surrounding parser
341358 -> GenParser Char ParserState Inline -- ^ content parser (to be used repeatedly)
342359simpleInline border construct = surrounded border (inlineWithAttribute) >>=
343360 return . construct . normalizeSpaces
344− 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
361+ where inlineWithAttribute = (try $ optional attributes) >> inline
tests/textile-reader.native+15−1
@@ -113,4 +113,18 @@ Pandoc (Meta {docTitle = [Str ""], docAuthors = [[Str ""]], docDate = [Str ""]})
113113 , [ Plain [Str "sex"] ] ],
114114 [ [ Plain [Str "joan"] ]
115115 , [ 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.
161161table{foo:bar}.
162162| name | age | sex |
163163| 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 ?