git.iko.soy/typeable/pandoc
on master help sign in
textile reader now ignores html/css attributes
commitfa0866886be778b0157340ea0563b6744424cfa6 authorPaul Rivier <paul.r.ml@gmail.com> dateSun 7 Nov 2010, 19:58 (16 years ago) parente6dde36
3 files changed+60−9
src/Text/Pandoc/Readers/Textile.hs+34−8 tests/textile-reader.native+14−1 tests/textile-reader.textile+12
src/Text/Pandoc/Readers/Textile.hs+34−8
@@ -26,9 +26,9 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2626 Portability : portable
2727
2828Conversion from Textile to 'Pandoc' document, based on the spec
29−available at http://redcloth.org/hobix.com/textile/
29+available at ahttp://redcloth.org/hobix.com/textile/
3030
31−Implemented :
31+Implemented and parsed :
3232 - Paragraphs
3333 - Code blocks
3434 - Lists
@@ -36,8 +36,8 @@ Implemented :
3636 - Inlines : strong, emph, cite, code, deleted, superscript,
3737 subscript, links
3838
39−Not implemented :
40− - HTML-specific and CSS-specific inlines
39+Implemented but discarded :
40+ - HTML-specific and CSS-specific attributes
4141-}
4242
4343
@@ -83,8 +83,8 @@ blockParsers = [ codeBlock
8383 , header
8484 , blockQuote
8585 , anyList
86− , table
87− , para
86+ , maybeExplicitBlock "table" table
87+ , maybeExplicitBlock "p" para
8888 , nullBlock ]
8989
9090-- | Any block in the order of definition of blockParsers
@@ -103,6 +103,7 @@ header :: GenParser Char ParserState Block
103103header = try $ do
104104 char 'h'
105105 level <- oneOf "123456" >>= return . digitToInt
106+ optional attributes
106107 char '.'
107108 whitespace
108109 name <- manyTill inline blockBreak
@@ -111,7 +112,9 @@ header = try $ do
111112-- | Blockquote of the form "bq. content"
112113blockQuote :: GenParser Char ParserState Block
113114blockQuote = try $ do
114− string "bq."
115+ string "bq"
116+ optional attributes
117+ char '.'
115118 whitespace
116119 para >>= return . BlockQuote . (:[])
117120
@@ -143,6 +146,7 @@ bulletListAtDepth depth = try $ do
143146bulletListItemAtDepth :: Int -> GenParser Char ParserState [Block]
144147bulletListItemAtDepth depth = try $ do
145148 count depth (char '*')
149+ optional attributes
146150 whitespace
147151 p <- inlines >>= return . Plain
148152 sublist <- option [] (anyListAtDepth (depth + 1) >>= return . (:[]))
@@ -160,6 +164,7 @@ orderedListAtDepth depth = try $ do
160164orderedListItemAtDepth :: Int -> GenParser Char ParserState [Block]
161165orderedListItemAtDepth depth = try $ do
162166 count depth (char '#')
167+ optional attributes
163168 whitespace
164169 p <- inlines >>= return . Plain
165170 sublist <- option [] (anyListAtDepth (depth + 1) >>= return . (:[]))
@@ -169,6 +174,7 @@ orderedListItemAtDepth depth = try $ do
169174blockBreak :: GenParser Char ParserState ()
170175blockBreak = try $ newline >> blanklines >> return ()
171176
177+
172178-- | In textile, paragraphs are separated by blank lines.
173179para :: GenParser Char ParserState Block
174180para = try $ do
@@ -223,6 +229,17 @@ table = try $ do
223229 rows
224230
225231
232+-- | Blocks like 'p' and 'table' do not need explicit block tag.
233+-- However, they can be used to set HTML/CSS attributes when needed.
234+maybeExplicitBlock :: String -- ^ block tag name
235+ -> GenParser Char ParserState Block -- ^ implicit block
236+ -> GenParser Char ParserState Block
237+maybeExplicitBlock name blk = try $ do
238+ optional $ string name >> optional attributes >> char '.' >>
239+ ((try whitespace) <|> endline)
240+ blk
241+
242+
226243
227244----------
228245-- Inlines
@@ -305,6 +322,13 @@ code :: GenParser Char ParserState Inline
305322code = surrounded (char '@') anyChar >>=
306323 return . Code
307324
325+
326+-- | Html / CSS attributes
327+attributes :: GenParser Char ParserState String
328+attributes = choice [ enclosed (char '(') (char ')') anyChar,
329+ enclosed (char '{') (char '}') anyChar,
330+ enclosed (char '[') (char ']') anyChar]
331+
308332-- | Parses material surrounded by a parser.
309333surrounded :: GenParser Char st t -- ^ surrounding parser
310334 -> GenParser Char st a -- ^ content parser (to be used repeatedly)
@@ -315,8 +339,9 @@ surrounded border = enclosed border border
315339simpleInline :: GenParser Char ParserState t -- ^ surrounding parser
316340 -> ([Inline] -> Inline) -- ^ Inline constructor
317341 -> GenParser Char ParserState Inline -- ^ content parser (to be used repeatedly)
318−simpleInline border construct = surrounded border inline >>=
342+simpleInline border construct = surrounded border (inlineWithAttribute) >>=
319343 return . construct . normalizeSpaces
344+ where inlineWithAttribute = (try $ optional attributes) >> inline
320345
321346
322347-- TODO
@@ -324,3 +349,4 @@ simpleInline border construct = surrounded border inline >>=
324349-- - Pandoc Meta Information (title, author, date)
325350-- - footnotes
326351-- - should autolink be shared through Parsing.hs ?
352+-- - embeded HTML, both inlines and blocks
tests/textile-reader.native+14−1
@@ -100,4 +100,17 @@ Pandoc (Meta {docTitle = [Str ""], docAuthors = [[Str ""]], docDate = [Str ""]})
100100 , [ Plain [Str "45"] ]
101101 , [ Plain [Str "f"] ] ] ]
102102, Header 1 [Str "Images"]
103−, Para [Str "Textile",Space,Str "inline",Space,Str "image",Space,Str "syntax,",Space,Str "like",Space,Str "here",Space,Image [Str "this is the alt text"] ("this_is_an_image.png","this is the alt text"),Space,Str "and",Space,Str "here",Space,Image [Str ""] ("this_is_an_image.png",""),Str "."] ]
103+, Para [Str "Textile",Space,Str "inline",Space,Str "image",Space,Str "syntax,",Space,Str "like",Space,Str "here",Space,Image [Str "this is the alt text"] ("this_is_an_image.png","this is the alt text"),Space,Str "and",Space,Str "here",Space,Image [Str ""] ("this_is_an_image.png",""),Str "."]
104+, Header 1 [Str "Attributes"]
105+, Header 2 [Str "HTML",Space,Str "and",Space,Str "CSS",Space,Str "attributes",Space,Str "are",Space,Str "ignored"]
106+, Para [Str "as",Space,Str "well",Space,Str "as",Space,Strong [Str "inline",Space,Str "attributes"]]
107+, Para [Str "and",Space,Str "paragraph",Space,Str "attributes,",Space,Str "and",Space,Str "table",Space,Str "attributes."]
108+, Table [] [AlignDefault,AlignDefault,AlignDefault] [0.0,0.0,0.0]
109+ [
110+ ] [
111+ [ [ Plain [Str "name"] ]
112+ , [ Plain [Str "age"] ]
113+ , [ Plain [Str "sex"] ] ],
114+ [ [ Plain [Str "joan"] ]
115+ , [ Plain [Str "24"] ]
116+ , [ Plain [Str "f"] ] ] ] ]
tests/textile-reader.textile+12
@@ -149,3 +149,15 @@ h1. Images
149149Textile inline image syntax, like
150150here !this_is_an_image.png(this is the alt text)!
151151and here !this_is_an_image.png!.
152+
153+h1. Attributes
154+
155+h2{color:red}. HTML and CSS attributes are ignored
156+
157+as well as *(foo)inline attributes*
158+
159+p{color:green}. and paragraph attributes, and table attributes.
160+
161+table{foo:bar}.
162+| name | age | sex |
163+| joan | 24 | f |