From 4f572ddf6941b8fb0ad7a5835216c708998444f0 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Fri, 5 Apr 2019 10:04:28 -0700
Subject: [PATCH] Vimwiki reader: improve handling of internal links.

1) Don't append `.html`
2) Add `wikilink` title

This mirrors behavior of other wiki readers.  Generally the
`.html` extension is not wanted.  It may be important for
output to HTML in certain circumstances, but it can always
be added using a filter that matches on links with title
`wikilink`.

Note that if you have a workflow that uses pandoc to convert
vimwiki to readable HTML pages, you may need to add such a
filter to reproduce current behavior.

Here is a filter that does the job:

```lua
function Link(el)
    if el.title == 'wikilink' then
      el.target = el.target .. ".html"
    end
    return el
end
```

Save this as `fixlinks.lua` and use with `--lua-filter fixlinks.lua`.

Closes #5414.
---
 src/Text/Pandoc/Readers/Vimwiki.hs | 17 ++++++++++++-----
 test/vimwiki-reader.native         | 16 ++++++++--------
 2 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/src/Text/Pandoc/Readers/Vimwiki.hs b/src/Text/Pandoc/Readers/Vimwiki.hs
index 8b2863435..57fa6fa1a 100644
--- a/src/Text/Pandoc/Readers/Vimwiki.hs
+++ b/src/Text/Pandoc/Readers/Vimwiki.hs
@@ -74,7 +74,8 @@ import Text.Pandoc.Parsing (ParserState, ParserT, blanklines, emailAddress,
                             many1Till, orderedListMarker, readWithM,
                             registerHeader, spaceChar, stateMeta,
                             stateOptions, uri)
-import Text.Pandoc.Shared (crFilter, splitBy, stringify, stripFirstAndLast)
+import Text.Pandoc.Shared (crFilter, splitBy, stringify, stripFirstAndLast,
+                           isURI)
 import Text.Parsec.Char (alphaNum, anyChar, char, newline, noneOf, oneOf, space,
                          spaces, string)
 import Text.Parsec.Combinator (between, choice, count, eof, lookAhead, many1,
@@ -544,11 +545,17 @@ link = try $ do
                   False -> do
                     manyTill anyChar (string "]]")
 -- not using try here because [[hell]o]] is not rendered as a link in vimwiki
-                    return $ B.link (procLink contents) "" (B.str contents)
+                    let tit = if isURI contents
+                                 then ""
+                                 else "wikilink"
+                    return $ B.link (procLink contents) tit (B.str contents)
                   True  -> do
                     url <- manyTill anyChar $ char '|'
                     lab <- mconcat <$> manyTill inline (string "]]")
-                    return $ B.link (procLink url) "" lab
+                    let tit = if isURI url
+                                 then ""
+                                 else "wikilink"
+                    return $ B.link (procLink url) tit lab
 
 image :: PandocMonad m => VwParser m Inlines
 image = try $ do
@@ -580,13 +587,13 @@ images k
 procLink' :: String -> String
 procLink' s
   | take 6 s == "local:" = "file" ++ drop 5 s
-  | take 6 s == "diary:" = "diary/" ++ drop 6 s ++ ".html"
+  | take 6 s == "diary:" = "diary/" ++ drop 6 s
   | or ((`isPrefixOf` s) <$> [ "http:", "https:", "ftp:", "file:", "mailto:",
                               "news:", "telnet:" ])
                              = s
   | s == ""                  = ""
   | last s == '/'          = s
-  | otherwise                = s ++ ".html"
+  | otherwise                = s
 
 procLink :: String -> String
 procLink s = procLink' x ++ y
diff --git a/test/vimwiki-reader.native b/test/vimwiki-reader.native
index 2d720cc7e..3df91bec5 100644
--- a/test/vimwiki-reader.native
+++ b/test/vimwiki-reader.native
@@ -62,14 +62,14 @@ Pandoc (Meta {unMeta = fromList [("date",MetaInlines [Str "2017-05-01"]),("title
 ,Para [Link ("",[],[]) [Str "email",Space,Str "me"] ("mailto:info@example.org","")]
 ,Para [Link ("",[],[]) [Str "mailto:hello@bye.com"] ("mailto:hello@bye.com","")]
 ,Header 2 ("internal links",[],[]) [Str "internal",Space,Str "links"]
-,Para [Link ("",[],[]) [Str "This is a link"] ("This is a link.html","")]
-,Para [Link ("",[],[]) [Str "Description",Space,Str "of",Space,Str "the",Space,Str "link"] ("This is a link source.html","")]
-,Para [Link ("",[],[]) [Str "projects/Important Project 1"] ("projects/Important Project 1.html",""),SoftBreak,Link ("",[],[]) [Str "../index"] ("../index.html",""),SoftBreak,Link ("",[],[]) [Str "Other",Space,Str "files"] ("a subdirectory/","")]
-,Para [Link ("",[],[]) [Str "try",Space,Str "me",Space,Str "to",Space,Str "test",Space,Str "tag",Space,Str "anchors"] ("#tag-one","")]
-,Para [Link ("",[],[]) [Str "try",Space,Str "me",Space,Str "to",Space,Str "test",Space,Str "header",Space,Str "anchors"] ("#block quotes","")]
-,Para [Link ("",[],[]) [Str "try",Space,Str "me",Space,Str "to",Space,Str "test",Space,Str "strong",Space,Str "anchors"] ("#strong","")]
-,Para [Link ("",[],[]) [Str "Tasks",Space,Str "for",Space,Str "tomorrow"] ("Todo List.html#Tomorrow","")]
-,Para [Link ("",[],[]) [Str "diary:2017-05-01"] ("diary/2017-05-01.html","")]
+,Para [Link ("",[],[]) [Str "This is a link"] ("This is a link","wikilink")]
+,Para [Link ("",[],[]) [Str "Description",Space,Str "of",Space,Str "the",Space,Str "link"] ("This is a link source","wikilink")]
+,Para [Link ("",[],[]) [Str "projects/Important Project 1"] ("projects/Important Project 1","wikilink"),SoftBreak,Link ("",[],[]) [Str "../index"] ("../index","wikilink"),SoftBreak,Link ("",[],[]) [Str "Other",Space,Str "files"] ("a subdirectory/","wikilink")]
+,Para [Link ("",[],[]) [Str "try",Space,Str "me",Space,Str "to",Space,Str "test",Space,Str "tag",Space,Str "anchors"] ("#tag-one","wikilink")]
+,Para [Link ("",[],[]) [Str "try",Space,Str "me",Space,Str "to",Space,Str "test",Space,Str "header",Space,Str "anchors"] ("#block quotes","wikilink")]
+,Para [Link ("",[],[]) [Str "try",Space,Str "me",Space,Str "to",Space,Str "test",Space,Str "strong",Space,Str "anchors"] ("#strong","wikilink")]
+,Para [Link ("",[],[]) [Str "Tasks",Space,Str "for",Space,Str "tomorrow"] ("Todo List#Tomorrow","wikilink")]
+,Para [Link ("",[],[]) [Str "diary:2017-05-01"] ("diary/2017-05-01","wikilink")]
 ,Para [Link ("",[],[]) [Str "Important",Space,Str "Data"] ("file:../assets/data.csv","")]
 ,Header 3 ("links with thumbnails",[],[]) [Str "links",Space,Str "with",Space,Str "thumbnails"]
 ,Para [Link ("",[],[]) [Image ("",[],[]) [Str ""] ("./movie.jpg","")] ("http://www.google.com","")]