Skip to content

Commit

Permalink
Merge branch 'release/0.1.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
smallhadroncollider committed Feb 7, 2019
2 parents 7ea1976 + e15b9ea commit 55b0ff0
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .bin/build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resolver="lts-13.2"
resolver="lts-13.6"
ghcv="8.6.3"

branch=$(git rev-parse --abbrev-ref HEAD)
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ Currently only supports `http://` and `https://` prefixed URLs

[Binaries for Mac and Linux are available](https://github.com/smallhadroncollider/brok/releases). Add the binary to a directory in your path (such as `/usr/local/bin`).

### Cabal

If you have `cabal` installed:

```bash
cabal install brok
```

Make sure you run `cabal update` if you haven't run it recently.

### Building

**Requirements**: [Stack](https://docs.haskellstack.org/en/stable/README/)
Expand Down
4 changes: 2 additions & 2 deletions package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: brok
version: 0.1.3.0
version: 0.1.4.0
github: "smallhadroncollider/brok"
license: BSD3
author: "Small Hadron Collider"
Expand All @@ -12,7 +12,7 @@ extra-source-files:

# Metadata used when publishing your package
synopsis: Finds broken links in text files
# category: Web
category: Command Line Tools

# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
Expand Down
9 changes: 6 additions & 3 deletions src/Brok/Parser/Attoparsec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ import ClassyPrelude
import Data.Attoparsec.Text

lexeme :: Parser a -> Parser a
lexeme p = many' space *> p <* many' space
lexeme p = skipSpace *> p <* skipSpace

tchar :: Char -> Parser Text
tchar ch = singleton <$> char ch

chopt :: Char -> Parser Text
chopt ch = option "" (tchar ch)

manyChars :: Parser Char -> Parser Text
manyChars p = pack <$> many1 p

concat3 :: (Monoid a) => a -> a -> a -> a
concat3 t1 t2 t3 = concat [t1, t2, t3]

concat4 :: (Monoid a) => a -> a -> a -> a -> a
concat4 t1 t2 t3 t4 = concat [t1, t2, t3, t4]
concat5 :: (Monoid a) => a -> a -> a -> a -> a -> a
concat5 t1 t2 t3 t4 t5 = concat [t1, t2, t3, t4, t5]

surround :: Char -> Char -> Parser Text -> Parser Text
surround open close parser = concat3 <$> tchar open <*> parser <*> tchar close
11 changes: 2 additions & 9 deletions src/Brok/Parser/DB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,10 @@ import ClassyPrelude
import Data.Attoparsec.Text

import Brok.Parser.Links (url)
import Brok.Types.Link
import Brok.Types.Link (URL)

line :: Parser (URL, Integer)
line = do
lnk <- url
_ <- char ' '
int <- many1 digit
_ <- char '\n'
case readMay int :: Maybe Integer of
Just i -> return (lnk, i)
Nothing -> fail "Unable to parse timestamp"
line = (,) <$> (url <* char ' ') <*> (decimal <* endOfLine)

entries :: Parser [(URL, Integer)]
entries = many1 line
Expand Down
21 changes: 16 additions & 5 deletions src/Brok/Parser/Links.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,30 @@ import Brok.Types.Link (URL)

type Token = Maybe URL

preQueryChars :: String
preQueryChars = "-._~:/#%@"

queryBodyChars :: String
queryBodyChars = "-._~:/#%@!$&*+,;="

chars :: String -> Parser Char
chars chrs = digit <|> letter <|> choice (char <$> chrs)

-- parentheses
parens :: Parser Text -> Parser Text
parens parser = surround '(' ')' parser <|> surround '[' ']' parser

-- urls
urlChar :: Parser Char
urlChar = digit <|> letter <|> choice (char <$> "-._~:/?#%@!$&*+,;=")
part :: String -> Parser Text
part str = concat <$> many' (parens (part str) <|> manyChars (chars str))

urlChars :: Parser Text
urlChars = concat <$> many1 (parens urlChars <|> (pack <$> many1 urlChar))
query :: Parser Text
query = (++) <$> string "?" <*> part queryBodyChars

url :: Parser Text
url = concat4 <$> string "http" <*> chopt 's' <*> string "://" <*> urlChars
url =
concat5 <$> string "http" <*> chopt 's' <*> string "://" <*> part preQueryChars <*>
option "" query

noise :: Parser Token
noise = anyChar >> return Nothing
Expand Down
2 changes: 1 addition & 1 deletion src/Brok/Parser/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ ignoreP :: Parser Option
ignoreP = lexeme $ Ignore <$> (string "--ignore" *> char '\n' *> many1 urlP)

fileP :: Parser Text
fileP = lexeme $ pack <$> many1 (notChar '\n')
fileP = lexeme $ manyChars (notChar '\n')

optsToConfig :: [Option] -> Config
optsToConfig = foldl' convert defaultConfig
Expand Down
64 changes: 2 additions & 62 deletions stack.yaml
Original file line number Diff line number Diff line change
@@ -1,64 +1,4 @@
# This file was automatically generated by 'stack init'
#
# Some commonly used options have been documented as comments in this file.
# For advanced use and comprehensive documentation of the format, please see:
# https://docs.haskellstack.org/en/stable/yaml_configuration/

# Resolver to choose a 'specific' stackage snapshot or a compiler version.
# A snapshot resolver dictates the compiler version and the set of packages
# to be used for project dependencies. For example:
#
# resolver: lts-3.5
# resolver: nightly-2015-09-21
# resolver: ghc-7.10.2
#
# The location of a snapshot can be provided as a file or url. Stack assumes
# a snapshot provided as a file might change, whereas a url resource does not.
#
# resolver: ./custom-snapshot.yaml
# resolver: https://example.com/snapshots/2018-01-01.yaml
resolver: lts-13.2

# User packages to be built.
# Various formats can be used as shown in the example below.
#
# packages:
# - some-directory
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
# - location:
# git: https://github.com/commercialhaskell/stack.git
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a
# subdirs:
# - auto-update
# - wai
resolver: lts-13.6
pvp-bounds: both
packages:
- .
# Dependency packages to be pulled from upstream that are not in the resolver
# using the same syntax as the packages field.
# (e.g., acme-missiles-0.3)
# extra-deps: []

# Override default flag values for local packages and extra-deps
# flags: {}

# Extra package databases containing global packages
# extra-package-dbs: []

# Control whether we use the GHC we find on the path
# system-ghc: true
#
# Require a specific version of stack, using version ranges
# require-stack-version: -any # Default
# require-stack-version: ">=1.9"
#
# Override the architecture used by stack, especially useful on Windows
# arch: i386
# arch: x86_64
#
# Extra directories used by stack for building
# extra-include-dirs: [/path/to/dir]
# extra-lib-dirs: [/path/to/dir]
#
# Allow a newer minor version of GHC than the snapshot specifies
# compiler-check: newer-minor
5 changes: 4 additions & 1 deletion test/Parser/DBTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ test_db =
])
(db content))
, testCase
"big file"
"big file (last)"
(assertEqual
"Gives back final url"
(Just
"https://developmentarc.gitbooks.io/react-indepth/content/life_cycle/the_life_cycle_recap.html")
(fst <$> (lastMay . fromRight [] $ db big)))
, testCase
"big file (length)"
(assertEqual "Gives back length" 142 (length $ fromRight [] (db big)))
, testCase "invalid .brokdb" (assertEqual "Parse error" True (isLeft $ db invalid))
, testCase "parses empty" (assertEqual "Gives back empty array" (Right []) (db ""))
]
22 changes: 22 additions & 0 deletions test/Parser/LinksTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ test_parser =
"Gives back google.com"
(Right ["https://google.com"])
(links "https://google.com"))
, testCase
"just long link"
(assertEqual
"Gives back full URL"
(Right
[ "https://developmentarc.gitbooks.io/react-indepth/content/life_cycle/the_life_cycle_recap.html"
])
(links
"https://developmentarc.gitbooks.io/react-indepth/content/life_cycle/the_life_cycle_recap.html"))
, testCase
"http link with surrounding text"
(assertEqual
Expand All @@ -61,6 +70,13 @@ test_parser =
(links "[A link](http://google.com)"))
, testCase
"link with brackets"
(assertEqual
"Gives back google.com"
(Right ["https://en.wikipedia.org/wiki/Word_(computer_architecture)"])
(links
"[A link](https://en.wikipedia.org/wiki/Word_(computer_architecture))"))
, testCase
"link with brackets in query"
(assertEqual
"Gives back google.com"
(Right ["http://google.com?q=(fish)"])
Expand Down Expand Up @@ -91,6 +107,12 @@ test_parser =
"Gives back google.com"
(Right ["http://google.com?q=fish"])
(links "'http://google.com?q=fish'"))
, testCase
"link with comma on end"
(assertEqual
"Gives back google.com"
(Right ["http://google.com"])
(links "testing http://google.com, here"))
]
, testGroup
"multiple links"
Expand Down

0 comments on commit 55b0ff0

Please sign in to comment.