Skip to content

Commit

Permalink
Textile reader/writer: Fixed autolinks.
Browse files Browse the repository at this point in the history
Previously the textile reader and writer incorrectly implented
RST-style autolinks for URLs and email addresses.

This has been fixed.  Now an autolink is done this way:

    "$":http://myurl.com
  • Loading branch information
jgm committed Nov 7, 2012
1 parent 4aa3e1f commit 4cd573c
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 19 deletions.
16 changes: 5 additions & 11 deletions src/Text/Pandoc/Readers/Textile.hs
Expand Up @@ -46,7 +46,6 @@ Left to be implemented:
- continued blocks (ex bq..)
TODO : refactor common patterns across readers :
- autolink
- more ...
-}
Expand Down Expand Up @@ -361,8 +360,7 @@ inline = choice inlineParsers <?> "inline"

-- | Inline parsers tried in order
inlineParsers :: [Parser [Char] ParserState Inline]
inlineParsers = [ autoLink
, str
inlineParsers = [ str
, whitespace
, endline
, code
Expand Down Expand Up @@ -501,21 +499,17 @@ linkNoB = try $ do
char ':'
let stopChars = "!.,;:"
url <- manyTill nonspaceChar (lookAhead $ space <|> try (oneOf stopChars >> (space <|> newline)))
return $ Link name (url, "")
let name' = if name == [Str "$"] then [Str url] else name
return $ Link name' (url, "")

linkB :: Parser [Char] ParserState Inline
linkB = try $ do
char '['
name <- surrounded (char '"') inline
char ':'
url <- manyTill nonspaceChar (char ']')
return $ Link name (url, "")

-- | Detect plain links to http or email.
autoLink :: Parser [Char] ParserState Inline
autoLink = do
(orig, src) <- (try uri <|> try emailAddress)
return $ Link [Str orig] (src, "")
let name' = if name == [Str "$"] then [Str url] else name
return $ Link name' (url, "")

-- | image embedding
image :: Parser [Char] ParserState Inline
Expand Down
5 changes: 4 additions & 1 deletion src/Text/Pandoc/Writers/Textile.hs
Expand Up @@ -396,7 +396,10 @@ inlineToTextile _ Space = return " "

inlineToTextile opts (Link txt (src, _)) = do
label <- case txt of
[Code _ s] -> return s
[Code _ s]
| s == src -> return "$"
[Str s]
| s == src -> return "$"
_ -> inlineListToTextile opts txt
return $ "\"" ++ label ++ "\":" ++ src

Expand Down
2 changes: 1 addition & 1 deletion templates
2 changes: 1 addition & 1 deletion tests/textile-reader.native
Expand Up @@ -85,7 +85,7 @@ Pandoc (Meta {docTitle = [], docAuthors = [], docDate = []})
,Header 2 [Str "Explicit"]
,Para [Str "Just",Space,Str "a",Space,Link [Str "url"] ("http://www.url.com","")]
,Para [Link [Str "Email",Space,Str "link"] ("mailto:nobody@nowhere.net","")]
,Para [Str "Automatic",Space,Str "linking",Space,Str "to",Space,Link [Str "http://www.example.com"] ("http://www.example.com",""),Space,Str "and",Space,Link [Str "foobar@example.com"] ("mailto:foobar@example.com",""),Str "."]
,Para [Str "Automatic",Space,Str "linking",Space,Str "to",Space,Link [Str "http://www.example.com"] ("http://www.example.com",""),Str "."]
,Para [Link [Str "Example"] ("http://www.example.com/",""),Str ":",Space,Str "Example",Space,Str "of",Space,Str "a",Space,Str "link",Space,Str "followed",Space,Str "by",Space,Str "a",Space,Str "colon",Str "."]
,Para [Str "A",Space,Str "link",Link [Str "with",Space,Str "brackets"] ("http://www.example.com",""),Str "and",Space,Str "no",Space,Str "spaces",Str "."]
,Header 1 [Str "Tables"]
Expand Down
2 changes: 1 addition & 1 deletion tests/textile-reader.textile
Expand Up @@ -157,7 +157,7 @@ Just a "url":http://www.url.com

"Email link":mailto:nobody@nowhere.net

Automatic linking to http://www.example.com and foobar@example.com.
Automatic linking to "$":http://www.example.com.

"Example":http://www.example.com/: Example of a link followed by a colon.

Expand Down
8 changes: 4 additions & 4 deletions tests/writer.textile
Expand Up @@ -650,15 +650,15 @@ Here's an "inline link in pointy braces":/script?foo=1&bar=2.

h2. Autolinks

With an ampersand: "http://example.com/?foo=1&bar=2":http://example.com/?foo=1&bar=2
With an ampersand: "$":http://example.com/?foo=1&bar=2

* In a list?
* "http://example.com/":http://example.com/
* "$":http://example.com/
* It should.

An e-mail address: "nobody@nowhere.net":mailto:nobody@nowhere.net
An e-mail address: "<tt>nobody@nowhere.net</tt>":mailto:nobody@nowhere.net

bq. Blockquoted: "http://example.com/":http://example.com/
bq. Blockquoted: "$":http://example.com/



Expand Down

0 comments on commit 4cd573c

Please sign in to comment.