aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Readers/Vimwiki.hs
blob: 74dac5ea791ccb0e5312e29d47b720b895b93fee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE CPP #-}
{- |
   Module      : Text.Pandoc.Readers.Vimwiki
   Copyright   : Copyright (C) 2017-2020 Yuchen Pei
   License     : GNU GPL, version 2 or above

   Maintainer  : Yuchen Pei <me@ypei.me>
   Stability   : alpha
   Portability : portable

Conversion of vimwiki text to 'Pandoc' document.
-}
{--
[X]: implemented
[O]: not implemented
* block parsers:
    * [X] header
    * [X] hrule
    * [X] comment
    * [X] blockquote
    * [X] preformatted -- using codeblock
    * [X] displaymath
    * [X] bulletlist / orderedlist
        * [X] todo lists -- using span.
    * [X] table
        * [X] centered table -- using div
        * [O] colspan and rowspan -- see issue #1024
    * [X] paragraph
    * [X] definition list
* inline parsers:
    * [X] bareURL
    * [X] strong
    * [X] emph
    * [X] strikeout
    * [X] code
    * [X] link
    * [X] image
    * [X] inline math
    * [X] tag
    * [X] sub- and super-scripts
* misc:
    * [X] `TODO:` mark
    * [X] metadata placeholders: %title and %date
    * [O] control placeholders: %template and %nohtml -- ignored
--}

module Text.Pandoc.Readers.Vimwiki ( readVimwiki
                                 ) where
import Control.Monad (guard)
import Control.Monad.Except (throwError)
import Data.Default
import Data.List (isInfixOf)
import Data.Maybe
import Data.Text (Text)
import qualified Data.Text as T
import Text.Pandoc.Builder (Blocks, Inlines, fromList, toList, trimInlines)
import qualified Text.Pandoc.Builder as B (blockQuote, bulletList, code,
                                           codeBlockWith, definitionList,
                                           displayMath, divWith, emph,
                                           headerWith, horizontalRule, image,
                                           imageWith, link, math, orderedList,
                                           para, plain, setMeta, simpleTable,
                                           softbreak, space, spanWith, str,
                                           strikeout, strong, subscript,
                                           superscript)
import Text.Pandoc.Class.PandocMonad (PandocMonad (..))
import Text.Pandoc.Definition (Attr, Block (BulletList, OrderedList),
                               Inline (Space), ListNumberDelim (..),
                               ListNumberStyle (..), Pandoc (..),
                               nullMeta)
import Text.Pandoc.Options (ReaderOptions)
import Text.Pandoc.Parsing (ParserState, ParserT, blanklines, emailAddress,
                            many1Till, orderedListMarker, readWithM,
                            registerHeader, spaceChar, stateMeta,
                            stateOptions, uri, manyTillChar, manyChar, textStr,
                            many1Char, countChar, many1TillChar)
import Text.Pandoc.Shared (crFilter, splitTextBy, stringify, stripFirstAndLast,
                           isURI, tshow)
import Text.Parsec.Char (alphaNum, anyChar, char, newline, noneOf, oneOf, space,
                         spaces, string)
import Text.Parsec.Combinator (between, choice, eof, lookAhead, many1,
                               manyTill, notFollowedBy, option, skipMany1)
import Text.Parsec.Prim (getState, many, try, updateState, (<|>))

readVimwiki :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
readVimwiki opts s = do
  res <- readWithM parseVimwiki def{ stateOptions = opts } $ crFilter s
  case res of
       Left e       -> throwError e
       Right result -> return result

type VwParser = ParserT Text ParserState


-- constants

specialChars :: [Char]
specialChars = "=*-#[]_~{}`$|:%^,"

spaceChars :: [Char]
spaceChars = " \t\n"

-- main parser

parseVimwiki :: PandocMonad m => VwParser m Pandoc
parseVimwiki = do
  bs <- mconcat <$> many block
  spaces
  eof
  st <- getState
  let meta = stateMeta st
  return $ Pandoc meta (toList bs)

-- block parser

block :: PandocMonad m => VwParser m Blocks
block = do
  res <- choice [ mempty <$ blanklines
                , header
                , hrule
                , mempty <$ comment
                , mixedList
                , preformatted
                , displayMath
                , table
                , mempty <$ placeholder
                , blockQuote
                , definitionList
                , para
                ]
  trace (T.take 60 $ tshow $ toList res)
  return res

blockML :: PandocMonad m => VwParser m Blocks
blockML = choice [preformatted, displayMath, table]

header :: PandocMonad m => VwParser m Blocks
header = try $ do
  sp <- many spaceChar
  eqs <- many1 (char '=')
  spaceChar
  let lev = length eqs
  guard $ lev <= 6
  contents <- trimInlines . mconcat <$> manyTill inline (try $ spaceChar
    >> string eqs >> many spaceChar >> newline)
  attr <- registerHeader (makeId contents,
    ["justcenter" | not (null sp)], []) contents
  return $ B.headerWith attr lev contents

para :: PandocMonad m => VwParser m Blocks
para = try $ do
  contents <- trimInlines . mconcat <$> many1 inline
  if all (==Space) (toList contents)
     then return mempty
     else return $ B.para contents

hrule :: PandocMonad m => VwParser m Blocks
hrule = try $ B.horizontalRule <$ (string "----" >> many (char '-') >> newline)

comment :: PandocMonad m => VwParser m ()
comment = try $ do
  many spaceChar >> string "%%" >> many (noneOf "\n")
  return ()

blockQuote :: PandocMonad m => VwParser m Blocks
blockQuote = try $ do
  string "    "
  contents <- trimInlines . mconcat <$> many1 inlineBQ
  if all (==Space) (toList contents)
     then return mempty
     else return $ B.blockQuote $ B.plain contents

definitionList :: PandocMonad m => VwParser m Blocks
definitionList = try $
  B.definitionList <$> many1 (dlItemWithDT <|> dlItemWithoutDT)

dlItemWithDT :: PandocMonad m => VwParser m (Inlines, [Blocks])
dlItemWithDT = do
  dt <- definitionTerm
  dds <- many definitionDef
  return (dt, dds)

dlItemWithoutDT :: PandocMonad m => VwParser m (Inlines, [Blocks])
dlItemWithoutDT = do
  dds <- many1 definitionDef
  return (mempty, dds)

definitionDef :: PandocMonad m => VwParser m Blocks
definitionDef = try $
  notFollowedBy definitionTerm >> many spaceChar
    >> (definitionDef1 <|> definitionDef2)

definitionDef1 :: PandocMonad m => VwParser m Blocks
definitionDef1 = try $ mempty <$ defMarkerE

definitionDef2 :: PandocMonad m => VwParser m Blocks
definitionDef2 = try $ B.plain <$>
  (defMarkerM >> (trimInlines . mconcat <$> many inline') <* newline)


definitionTerm :: PandocMonad m => VwParser m Inlines
definitionTerm = try $ do
  x <- definitionTerm1 <|> definitionTerm2
  guard (stringify x /= "")
  return x

definitionTerm1 :: PandocMonad m => VwParser m Inlines
definitionTerm1 = try $
  trimInlines . mconcat <$> manyTill inline' (try defMarkerE)

definitionTerm2 :: PandocMonad m => VwParser m Inlines
definitionTerm2 = try $ trimInlines . mconcat <$> manyTill inline'
  (try $lookAhead (defMarkerM >> notFollowedBy hasDefMarkerM))

defMarkerM :: PandocMonad m => VwParser m Char
defMarkerM = string "::" >> spaceChar

defMarkerE :: PandocMonad m => VwParser m Char
defMarkerE = string "::" >> newline

hasDefMarkerM :: PandocMonad m => VwParser m Text
hasDefMarkerM = manyTillChar (noneOf "\n") (try defMarkerM)

preformatted :: PandocMonad m => VwParser m Blocks
preformatted = try $ do
  many spaceChar >> string "{{{"
  attrText <- manyChar (noneOf "\n")
  lookAhead newline
  contents <- manyTillChar anyChar (try (char '\n' >> many spaceChar >> string "}}}"
    >> many spaceChar >> newline))
  if (contents /= "") && (T.head contents == '\n')
     then return $ B.codeBlockWith (makeAttr attrText) (T.tail contents)
     else return $ B.codeBlockWith (makeAttr attrText) contents

makeAttr :: Text -> Attr
makeAttr s =
  let xs = splitTextBy (`elem` (" \t" :: String)) s in
    ("", syntax xs, mapMaybe nameValue xs)

syntax :: [Text] -> [Text]
syntax (s:_) | not $ T.isInfixOf "=" s = [s]
syntax _ = []

nameValue :: Text -> Maybe (Text, Text)
nameValue s =
  let t = splitTextBy (== '=') s in
    if length t /= 2
      then Nothing
      else let (a, b) = (head t, last t) in
             if (T.length b < 2) || ((T.head b, T.last b) /= ('"', '"'))
               then Nothing
               else Just (a, stripFirstAndLast b)


displayMath :: PandocMonad m => VwParser m Blocks
displayMath = try $ do
  many spaceChar >> string "{{$"
  mathTag <- option "" mathTagParser
  many space
  contents <- manyTillChar anyChar (try (char '\n' >> many spaceChar >> string "}}$"
    >> many spaceChar >> newline))
  let contentsWithTags
        | mathTag == "" = contents
        | otherwise     = "\\begin{" <> mathTag <> "}\n" <> contents
                          <> "\n\\end{" <> mathTag <> "}"
  return $ B.para $ B.displayMath contentsWithTags


mathTagLaTeX :: Text -> Text
mathTagLaTeX s = case s of
   "equation"  -> ""
   "equation*" -> ""
   "gather"    -> "gathered"
   "gather*"   -> "gathered"
   "multline"  -> "gathered"
   "multline*" -> "gathered"
   "eqnarray"  -> "aligned"
   "eqnarray*" -> "aligned"
   "align"     -> "aligned"
   "align*"    -> "aligned"
   "alignat"   -> "aligned"
   "alignat*"  -> "aligned"
   _           -> s


mixedList :: PandocMonad m => VwParser m Blocks
mixedList = try $ do
  (bl, _) <- mixedList' (-1)
  return $ head bl

mixedList' :: PandocMonad m => Int -> VwParser m ([Blocks], Int)
mixedList' prevInd = do
  (curInd, builder) <- option (-1, "na") (lookAhead listStart)
  if curInd < prevInd
     then return ([], curInd)
     else do
          listStart
          curLine <- listItemContent
          let listBuilder =
                if builder == "ul" then B.bulletList else B.orderedList
          (subList, lowInd) <- mixedList' curInd
          if lowInd >= curInd
             then do
                  (sameIndList, endInd) <- mixedList' lowInd
                  let curList = combineList curLine subList ++ sameIndList
                  if curInd > prevInd
                     then return ([listBuilder curList], endInd)
                     else return (curList, endInd)
             else do
                  let (curList, endInd) = (combineList curLine subList,
                                           lowInd)
                  if curInd > prevInd
                     then return ([listBuilder curList], endInd)
                     else return (curList, endInd)

plainInlineML' :: PandocMonad m => Inlines -> VwParser m Blocks
plainInlineML' w = do
  xs <- many inlineML
  newline
  return $ B.plain $ trimInlines $ mconcat $ w:xs

plainInlineML :: PandocMonad m => VwParser m Blocks
plainInlineML = notFollowedBy listStart >> spaceChar >> plainInlineML' mempty


listItemContent :: PandocMonad m => VwParser m Blocks
listItemContent = try $ do
  w <- option mempty listTodoMarker
  x <- plainInlineML' w
  y <- many blocksThenInline
  z <- many blockML
  return $ mconcat $ x:y ++ z

blocksThenInline :: PandocMonad m => VwParser m Blocks
blocksThenInline = try $ do
  y <- many1 blockML
  x <- plainInlineML
  return $ mconcat $ y ++ [x]

listTodoMarker :: PandocMonad m => VwParser m Inlines
listTodoMarker = try $ do
  x <- between (many spaceChar >> char '[') (char ']' >> spaceChar)
    (oneOf " .oOX")
  return $ makeListMarkerSpan x

makeListMarkerSpan :: Char -> Inlines
makeListMarkerSpan x =
  let cl = case x of
            ' ' -> "done0"
            '.' -> "done1"
            'o' -> "done2"
            'O' -> "done3"
            'X' -> "done4"
            _   -> ""
    in
      B.spanWith ("", [cl], []) mempty

combineList :: Blocks -> [Blocks] -> [Blocks]
combineList x [y] = case toList y of
                            [BulletList z] -> [fromList $ toList x
                                              ++ [BulletList z]]
                            [OrderedList attr z] -> [fromList $ toList x
                                                    ++ [OrderedList attr z]]
                            _ -> x:[y]
combineList x xs = x:xs

listStart :: PandocMonad m => VwParser m (Int, Text)
listStart = try $ do
  s <- many spaceChar
  listType <- bulletListMarkers <|> orderedListMarkers
  spaceChar
  return (length s, listType)

bulletListMarkers :: PandocMonad m => VwParser m Text
bulletListMarkers = "ul" <$ (char '*' <|> char '-')

orderedListMarkers :: PandocMonad m => VwParser m Text
orderedListMarkers =
  ("ol" <$choice (orderedListMarker Decimal Period:(($OneParen) . orderedListMarker <$> [Decimal, LowerRoman, UpperRoman, LowerAlpha, UpperAlpha])))
    <|> ("ol" <$ char '#')

--many need trimInlines
table :: PandocMonad m => VwParser m Blocks
table = try $ do
  indent <- lookAhead (many spaceChar)
  (th, trs) <- table1 <|> table2
  let tab = B.simpleTable th trs
  if indent == ""
    then return tab
    else return $ B.divWith ("", ["center"], []) tab

-- table with header
table1 :: PandocMonad m => VwParser m ([Blocks], [[Blocks]])
table1 = try $ do
  th <- tableRow
  many1 tableHeaderSeparator
  trs <- many tableRow
  return (th, trs)

-- headerless table
table2 :: PandocMonad m => VwParser m ([Blocks], [[Blocks]])
table2 = try $ do
  trs <- many1 tableRow
  return (replicate (length $ head trs) mempty, trs)

tableHeaderSeparator :: PandocMonad m => VwParser m ()
tableHeaderSeparator = try $ do
  many spaceChar >> char '|' >> many1 (many1 (char '-') >> char '|')
    >> many spaceChar >> newline
  return ()

tableRow :: PandocMonad m => VwParser m [Blocks]
tableRow = try $ do
  many spaceChar >> char '|'
  s <- lookAhead $ manyTill anyChar (try (char '|' >> many spaceChar
    >> newline))
  guard $ not $ "||" `isInfixOf` ("|" ++ s ++ "|")
  many tableCell <* many spaceChar <* char '\n'

tableCell :: PandocMonad m => VwParser m Blocks
tableCell = try $
  B.plain . trimInlines . mconcat <$> manyTill inline' (char '|')

placeholder :: PandocMonad m => VwParser m ()
placeholder = try $
  choice (ph <$> ["title", "date"]) <|> noHtmlPh <|> templatePh

ph :: PandocMonad m => Text -> VwParser m ()
ph s = try $ do
  many spaceChar >> textStr (T.cons '%' s) >> spaceChar
  contents <- trimInlines . mconcat <$> manyTill inline (lookAhead newline)
    --use lookAhead because of placeholder in the whitespace parser
  let meta' = B.setMeta s contents nullMeta
  -- this order ensures that later values will be ignored in favor
  -- of earlier ones:
  updateState $ \st -> st { stateMeta = meta' <> stateMeta st }

noHtmlPh :: PandocMonad m => VwParser m ()
noHtmlPh = try $
  () <$ many spaceChar <* string "%nohtml" <* many spaceChar
    <* lookAhead newline

templatePh :: PandocMonad m => VwParser m ()
templatePh = try $
  () <$ many spaceChar <* string "%template" <* many (noneOf "\n")
    <* lookAhead newline

-- inline parser

inline :: PandocMonad m => VwParser m Inlines
inline = choice $ whitespace endlineP:inlineList

inlineList :: PandocMonad m => [VwParser m Inlines]
inlineList = [  bareURL
             ,  todoMark
             ,  str
             ,  strong
             ,  emph
             ,  strikeout
             ,  code
             ,  link
             ,  image
             ,  inlineMath
             ,  tag
             ,  superscript
             ,  subscript
             ,  special
             ]

-- inline parser without softbreaks or comment breaks
inline' :: PandocMonad m => VwParser m Inlines
inline' = choice $ whitespace':inlineList

-- inline parser for blockquotes
inlineBQ :: PandocMonad m => VwParser m Inlines
inlineBQ = choice $ whitespace endlineBQ:inlineList

-- inline parser for mixedlists
inlineML :: PandocMonad m => VwParser m Inlines
inlineML = choice $ whitespace endlineML:inlineList

str :: PandocMonad m => VwParser m Inlines
str = B.str <$> many1Char (noneOf $ spaceChars ++ specialChars)

whitespace :: PandocMonad m => VwParser m () -> VwParser m Inlines
whitespace endline = B.space <$ (skipMany1 spaceChar <|>
                                 try (newline >> (comment <|> placeholder)))
         <|> B.softbreak <$ endline

whitespace' :: PandocMonad m => VwParser m Inlines
whitespace' = B.space <$ skipMany1 spaceChar

special :: PandocMonad m => VwParser m Inlines
special = B.str <$> countChar 1 (oneOf specialChars)

bareURL :: PandocMonad m => VwParser m Inlines
bareURL = try $ do
  (orig, src) <- uri <|> emailAddress
  return $ B.link src "" (B.str orig)

strong :: PandocMonad m => VwParser m Inlines
strong = try $ do
  s <- lookAhead $ between (char '*') (char '*') (many1 $ noneOf "*")
  guard $ (head s `notElem` spaceChars)
             && (last s `notElem` spaceChars)
  char '*'
  contents <- mconcat <$>manyTill inline' (char '*'
    >> notFollowedBy alphaNum)
  return $ B.spanWith (makeId contents, [], []) mempty
    <> B.strong contents

makeId :: Inlines -> Text
makeId i = T.concat (stringify <$> toList i)

emph :: PandocMonad m => VwParser m Inlines
emph = try $ do
  s <- lookAhead $ between (char '_') (char '_') (many1 $ noneOf "_")
  guard $ (head s `notElem` spaceChars)
          && (last s `notElem` spaceChars)
  char '_'
  contents <- mconcat <$>manyTill inline' (char '_'
    >> notFollowedBy alphaNum)
  return $ B.emph contents

strikeout :: PandocMonad m => VwParser m Inlines
strikeout = try $ do
  string "~~"
  contents <- mconcat <$>many1Till inline' (string "~~")
  return $ B.strikeout contents

code :: PandocMonad m => VwParser m Inlines
code = try $ do
  char '`'
  contents <- many1TillChar (noneOf "\n") (char '`')
  return $ B.code contents

superscript :: PandocMonad m => VwParser m Inlines
superscript = try $
  B.superscript . mconcat <$> (char '^' >> many1Till inline' (char '^'))

subscript :: PandocMonad m => VwParser m Inlines
subscript = try $
  B.subscript . mconcat <$> (string ",,"
    >> many1Till inline' (try $ string ",,"))

link :: PandocMonad m => VwParser m Inlines
link = try $ do
  string "[["
  contents <- lookAhead $ manyTillChar anyChar (string "]]")
  if T.any (== '|') contents
                  then do
                    url <- manyTillChar anyChar $ char '|'
                    lab <- mconcat <$> manyTill inline (string "]]")
                    let tit = if isURI url
                                 then ""
                                 else "wikilink"
                    return $ B.link (procLink url) tit lab
                  else do
                    manyTill anyChar (string "]]")
-- not using try here because [[hell]o]] is not rendered as a link in vimwiki
                    let tit = if isURI contents
                                 then ""
                                 else "wikilink"
                    return $ B.link (procLink contents) tit (B.str contents)

image :: PandocMonad m => VwParser m Inlines
image = try $ do
  string "{{"
  contentText <- lookAhead $ manyTill (noneOf "\n") (try $ string "}}")
  images $ length $ filter (== '|') contentText

images :: PandocMonad m => Int -> VwParser m Inlines
images k
  | k == 0 = do
           imgurl <- manyTillChar anyChar (try $ string "}}")
           return $ B.image (procImgurl imgurl) "" (B.str "")
  | k == 1 = do
           imgurl <- manyTillChar anyChar (char '|')
           alt <- mconcat <$> manyTill inline (try $ string "}}")
           return $ B.image (procImgurl imgurl) "" alt
  | k == 2 = do
           imgurl <- manyTillChar anyChar (char '|')
           alt <- mconcat <$> manyTill inline (char '|')
           attrText <- manyTillChar anyChar (try $ string "}}")
           return $ B.imageWith (makeAttr attrText) (procImgurl imgurl) "" alt
  | otherwise = do
           imgurl <- manyTillChar anyChar (char '|')
           alt <- mconcat <$> manyTill inline (char '|')
           attrText <- manyTillChar anyChar (char '|')
           manyTill anyChar (try $ string "}}")
           return $ B.imageWith (makeAttr attrText) (procImgurl imgurl) "" alt

procLink' :: Text -> Text
procLink' s
  | T.take 6 s == "local:" = "file" <> T.drop 5 s
  | T.take 6 s == "diary:" = "diary/" <> T.drop 6 s
  | or ((`T.isPrefixOf` s) <$> [ "http:", "https:", "ftp:", "file:", "mailto:",
                              "news:", "telnet:" ])
                             = s
  | s == ""                  = ""
  | T.last s == '/'          = s
  | otherwise                = s

procLink :: Text -> Text
procLink s = procLink' x <> y
  where (x, y) = T.break (=='#') s

procImgurl :: Text -> Text
procImgurl s = if T.take 6 s == "local:" then "file" <> T.drop 5 s else s

inlineMath :: PandocMonad m => VwParser m Inlines
inlineMath = try $
  B.math <$ char '$' <*> many1TillChar (noneOf "\n") (char '$')

tag :: PandocMonad m => VwParser m Inlines
tag = try $ do
  char ':'
  s <- manyTillChar (noneOf spaceChars) (try (char ':' >> lookAhead space))
  guard $ not $ "::" `T.isInfixOf` (":" <> s <> ":")
  let ss = splitTextBy (==':') s
  return $ mconcat $ makeTagSpan' (head ss):(makeTagSpan <$> tail ss)

todoMark :: PandocMonad m => VwParser m Inlines
todoMark = try $ do
  string "TODO:"
  return $ B.spanWith ("", ["todo"], []) (B.str "TODO:")

-- helper functions and parsers
endlineP :: PandocMonad m => VwParser m ()
endlineP = () <$ try (newline <* nFBTTBSB <* notFollowedBy blockQuote)

endlineBQ :: PandocMonad m => VwParser m ()
endlineBQ = () <$ try (newline <* nFBTTBSB <* string "    ")

endlineML :: PandocMonad m => VwParser m ()
endlineML = () <$ try (newline <* nFBTTBSB <* many1 spaceChar)

--- nFBTTBSB is short for notFollowedByThingsThatBreakSoftBreaks
nFBTTBSB :: PandocMonad m => VwParser m ()
nFBTTBSB =
    notFollowedBy newline <*
    notFollowedBy hrule <*
    notFollowedBy tableRow <*
    notFollowedBy header <*
    notFollowedBy listStart <*
    notFollowedBy preformatted <*
    notFollowedBy displayMath <*
    notFollowedBy hasDefMarker

hasDefMarker :: PandocMonad m => VwParser m ()
hasDefMarker = () <$ manyTill (noneOf "\n") (string "::" >> oneOf spaceChars)

makeTagSpan' :: Text -> Inlines
makeTagSpan' s = B.spanWith (T.cons '-' s, [], []) (B.str "") <>
                  B.spanWith (s, ["tag"], []) (B.str s)

makeTagSpan :: Text -> Inlines
makeTagSpan s = B.space <> makeTagSpan' s

mathTagParser :: PandocMonad m => VwParser m Text
mathTagParser = do
  s <- try $ lookAhead (char '%' >> manyTillChar (noneOf spaceChars)
    (try $ char '%' >> many (noneOf $ '%':spaceChars) >> space))
  char '%' >> textStr s >> char '%'
  return $ mathTagLaTeX s