1
1
mirror of https://github.com/aelve/guide.git synced 2024-12-23 04:42:24 +03:00

Don't use unsafe T.head (and don't fail on empty links)

This commit is contained in:
Artyom 2017-11-02 03:27:55 +03:00
parent 83905bc08d
commit d6010dfcd5
No known key found for this signature in database
GPG Key ID: B8E35A33FF522710
3 changed files with 9 additions and 3 deletions

1
.gitignore vendored
View File

@ -35,4 +35,5 @@ tags
# JavaScript # JavaScript
guidejs/node_modules/ guidejs/node_modules/
front/node_modules/
static/js/ static/js/

View File

@ -62,10 +62,10 @@ consolidate s@("-":_) =
consolidate (".":".":".":xs) = "..." : consolidate xs consolidate (".":".":".":xs) = "..." : consolidate xs
-- links -- links
consolidate s@("http":":":"/":"/":_) = consolidate s@("http":":":"/":"/":_) =
let (l, r) = span (\x -> x /= ")" && not (isSpace (T.head x))) s let (l, r) = span (\x -> x /= ")" && not (startsWithSpace x)) s
in T.concat l : consolidate r in T.concat l : consolidate r
consolidate s@("https":":":"/":"/":_) = consolidate s@("https":":":"/":"/":_) =
let (l, r) = span (\x -> x /= ")" && not (isSpace (T.head x))) s let (l, r) = span (\x -> x /= ")" && not (startsWithSpace x)) s
in T.concat l : consolidate r in T.concat l : consolidate r
consolidate ("(":"@":"hk":")":xs) = "(" : "@hk" : ")" : consolidate xs consolidate ("(":"@":"hk":")":xs) = "(" : "@hk" : ")" : consolidate xs
@ -88,3 +88,8 @@ op = over _1 mconcat . span (isOpToken . T.unpack)
where where
isOpToken [c] = c `elem` (":!#$%&*+./<=>?@\\^|-~" :: String) isOpToken [c] = c `elem` (":!#$%&*+./<=>?@\\^|-~" :: String)
isOpToken _ = False isOpToken _ = False
startsWithSpace :: Text -> Bool
startsWithSpace s = case T.uncons s of
Nothing -> False
Just (c, _) -> isSpace c

View File

@ -189,7 +189,7 @@ extractInlines = concatMap go
CODE_BLOCK _ xs -> [MD.Node Nothing (CODE xs) []] CODE_BLOCK _ xs -> [MD.Node Nothing (CODE xs) []]
shortcutLinks :: MD.Node -> MD.Node shortcutLinks :: MD.Node -> MD.Node
shortcutLinks node@(MD.Node pos (LINK url title) ns) | '@' <- T.head url = shortcutLinks node@(MD.Node pos (LINK url title) ns) | "@" <- T.take 1 url =
-- %20s are possibly introduced by cmark (Pandoc definitely adds them, -- %20s are possibly introduced by cmark (Pandoc definitely adds them,
-- no idea about cmark but better safe than sorry) and so they need to -- no idea about cmark but better safe than sorry) and so they need to
-- be converted back to spaces -- be converted back to spaces