Skip to content
This repository was archived by the owner on Feb 5, 2023. It is now read-only.

Commit 887afac

Browse files
committed
Initial commit
0 parents  commit 887afac

File tree

12 files changed

+411
-0
lines changed

12 files changed

+411
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.stack-work
2+
/*.cabal

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
__Elm Proofread.__
2+
_This command-line tool runs your Elm documentation tests._
3+
4+
5+
```shell
6+
elm-proofread src/Main.elm
7+
```

bin/Main.hs

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
module Main where
2+
3+
import Data.Maybe (listToMaybe)
4+
import Data.Text (Text)
5+
import Proofread (Result(..))
6+
import Protolude
7+
import System.Console.Pretty
8+
import System.Environment (getArgs)
9+
10+
import qualified Data.Text as Text
11+
import qualified Proofread
12+
13+
14+
-- 🍯
15+
16+
17+
main :: IO ()
18+
main = do
19+
args <- getArgs
20+
21+
-- Fun w/ flags
22+
let maybeFilePath = listToMaybe (excludeFlags args)
23+
24+
-- Format!
25+
case maybeFilePath of
26+
Just filePath -> formatFile filePath
27+
Nothing -> putError "Argument missing"
28+
29+
30+
31+
-- 📮
32+
33+
34+
{-| Format contents from a file.
35+
-}
36+
formatFile :: [Char] -> IO ()
37+
formatFile filePath = do
38+
contents <- readFile filePath
39+
40+
let prefix = Text.pack (filePath ++ "")
41+
42+
case traceShowId (Proofread.proofread contents) of
43+
Ok result -> putSuccess (prefix <> "")
44+
Err err -> putError (prefix <> "") >> putError err >> exitFailure
45+
46+
47+
48+
-- ⛳️
49+
50+
51+
excludeFlags :: [[Char]] -> [[Char]]
52+
excludeFlags = filter excludeFlag
53+
54+
55+
excludeFlag :: [Char] -> Bool
56+
excludeFlag ('-' : _) = False
57+
excludeFlag _ = True
58+
59+
60+
61+
-- 🚦
62+
63+
64+
putError :: Text -> IO ()
65+
putError err =
66+
putStr ( color Red err )
67+
68+
69+
putSuccess :: Text -> IO ()
70+
putSuccess msg =
71+
putStr ( color Green msg )

package.yaml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: elm-proofread
2+
version: 0.1.0
3+
synopsis: Elm documentation testing.
4+
description: See README at <https://github.com/icidasset/elm-proofread#readme>
5+
maintainer: Steven Vandevelde <[email protected]>
6+
license: MIT
7+
github: icidasset/elm-proofread
8+
category: Development
9+
10+
11+
default-extensions:
12+
DisambiguateRecordFields
13+
DuplicateRecordFields
14+
NoImplicitPrelude
15+
OverloadedStrings
16+
17+
18+
ghc-options:
19+
-Wall
20+
-Wcompat
21+
-Wincomplete-record-updates
22+
-Wincomplete-uni-patterns
23+
-Wredundant-constraints
24+
25+
26+
dependencies:
27+
- base >= 4.7 && < 5
28+
- flow == 1.*
29+
- megaparsec == 6.5.*
30+
- parser-combinators == 1.*
31+
- pretty-terminal
32+
- protolude == 0.2.*
33+
- text == 1.*
34+
35+
36+
library:
37+
source-dirs: src
38+
exposed-modules:
39+
Proofread
40+
41+
42+
executables:
43+
elm-proofread:
44+
main: Main.hs
45+
source-dirs: bin
46+
dependencies:
47+
- elm-proofread

src/Proofread.hs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{-
2+
3+
ELM PROOFREAD
4+
=============
5+
6+
-}
7+
module Proofread
8+
( Document(..)
9+
, Result(..)
10+
, Test(..)
11+
, proofread
12+
) where
13+
14+
import Flow
15+
import Proofread.Types
16+
import Protolude
17+
18+
import qualified Data.Text as Text
19+
import qualified Proofread.Parser as Parser
20+
21+
22+
-- 🌳
23+
24+
25+
data Result
26+
= Ok Document
27+
| Err Text
28+
deriving (Show)
29+
30+
31+
32+
-- 📮
33+
34+
35+
proofread :: Text -> Result
36+
proofread contents =
37+
case Parser.parse contents of
38+
Parser.Ok document ->
39+
Ok document
40+
41+
Parser.Err err ->
42+
Err err

src/Proofread/Parser.hs

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
{-
2+
3+
PARSER
4+
======
5+
6+
-}
7+
module Proofread.Parser
8+
( Proofread.Parser.parse
9+
, Result(..)
10+
) where
11+
12+
import Control.Monad.Combinators
13+
import Data.Text (Text)
14+
import Flow
15+
import Proofread.Parser.Types
16+
import Proofread.Parser.Utilities
17+
import Proofread.Types
18+
import Protolude hiding (and, one, or, some, try)
19+
import Text.Megaparsec
20+
import Text.Megaparsec.Char
21+
22+
import qualified Data.List as List
23+
import qualified Data.Text as Text
24+
import qualified Text.Megaparsec as Mega
25+
26+
27+
-- 🌳
28+
29+
30+
data Result
31+
= Ok Document
32+
| Err Text
33+
deriving (Show)
34+
35+
36+
37+
-- ⚡️
38+
39+
40+
parse :: Text -> Result
41+
parse contents =
42+
contents
43+
|> Text.unpack
44+
|> Mega.parse document ""
45+
|> either (parseErrorPretty .> Text.pack .> Err) Ok
46+
47+
48+
49+
-- 📮
50+
51+
52+
document :: Parser Document
53+
document = do
54+
_ <- maybeSome whitespace
55+
m <- one docModule
56+
_ <- maybeSome whitespace
57+
t <- maybeSome test
58+
59+
return $ Document m t
60+
61+
62+
63+
-- MODULE
64+
65+
66+
docModule :: Parser Text
67+
docModule = do
68+
_ <- one (string "module ")
69+
moduleName <- some (alphaNumChar `or` char '.' `or` char '_')
70+
_ <- one spaceCharacter
71+
_ <- manyTill anyChar (string "\n\n")
72+
73+
return $ Text.pack moduleName
74+
75+
76+
77+
-- TESTS
78+
79+
80+
test :: Parser Test
81+
test =
82+
try (skipManyTill anyChar testInMultiLineComment)
83+
84+
85+
86+
{-| Parser for a test in a multiline comment.
87+
-}
88+
testInMultiLineComment :: Parser Test
89+
testInMultiLineComment = do
90+
_ <- one (string " >>> ")
91+
startInput <- someTill anyChar eol
92+
additionalInput <- maybeSome (try mlExtra)
93+
_ <- maybeSome whitespace
94+
expectedOutput <- manyTill anyChar (try mlEnd)
95+
96+
return $ Test
97+
{ input =
98+
additionalInput
99+
|> (<>) [ startInput, if length additionalInput > 0 then "\n" else "" ]
100+
|> List.concat
101+
|> Text.pack
102+
103+
, output =
104+
Text.pack expectedOutput
105+
}
106+
107+
108+
mlExtra :: Parser [Char]
109+
mlExtra = do
110+
_ <- some whitespace
111+
_ <- one (string "..> ")
112+
input <- manyTill anyChar eol
113+
114+
return input
115+
116+
117+
mlEnd :: Parser [Char]
118+
mlEnd =
119+
eol `andThen` maybeSome spaceCharacter `andThen` (eol `or` string "-}")

src/Proofread/Parser/Types.hs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Proofread.Parser.Types where
2+
3+
import Data.Void (Void)
4+
import Protolude (Char)
5+
import Text.Megaparsec (Parsec)
6+
7+
8+
-- 🌳
9+
10+
11+
type Parser = Parsec Void [Char]

src/Proofread/Parser/Utilities.hs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module Proofread.Parser.Utilities where
2+
3+
import Control.Applicative (Alternative, (<|>))
4+
import Prelude hiding (or)
5+
import Proofread.Parser.Types
6+
import Text.Megaparsec.Char
7+
8+
import qualified Control.Applicative
9+
10+
11+
-- ⚗️ Combinators
12+
13+
14+
and :: (Applicative f, Monoid a) => f a -> f a -> f a
15+
and =
16+
Control.Applicative.liftA2 mappend
17+
18+
19+
andThen :: Monad m => m a -> m b -> m b
20+
andThen =
21+
(>>)
22+
23+
24+
or :: Alternative f => f a -> f a -> f a
25+
or =
26+
(<|>)
27+
28+
29+
one :: a -> a
30+
one =
31+
id
32+
33+
34+
maybeSome :: Alternative f => f a -> f [a]
35+
maybeSome =
36+
Control.Applicative.many
37+
38+
39+
40+
-- 🤖 Predefined combinations
41+
42+
43+
spaceCharacter :: Parser Char
44+
spaceCharacter =
45+
char ' '
46+
47+
48+
whitespace :: Parser Char
49+
whitespace =
50+
spaceChar

src/Proofread/Types.hs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module Proofread.Types where
2+
3+
import Data.Text (Text)
4+
import Protolude (Show)
5+
6+
7+
-- 🌳
8+
9+
10+
data Document =
11+
Document Text [ Test ]
12+
deriving (Show)
13+
14+
15+
data Test = Test
16+
{ input :: Text, output :: Text }
17+
deriving (Show)

stack.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
resolver: lts-12.16
2+
extra-deps: [ pretty-terminal-0.1.0.0 ]

0 commit comments

Comments
 (0)