Added some modest improvements to code.

This commit is contained in:
Jan de Muijnck-Hughes 2020-06-11 09:57:19 +01:00 committed by G. Allais
parent ef85348de8
commit 49b4f97c5d

View File

@ -62,28 +62,28 @@ rawTokens delims ls =
++ [(notCodeLine, Any)] ++ [(notCodeLine, Any)]
||| Merge the tokens into a single source file. ||| Merge the tokens into a single source file.
reduce : List (TokenData Token) -> String -> String reduce : List (TokenData Token) -> List String -> String
reduce [] acc = acc reduce [] acc = fastAppend (reverse acc)
reduce (MkToken _ _ (Any x) :: rest) acc = reduce rest (acc ++ blank_content) reduce (MkToken _ _ (Any x) :: rest) acc = reduce rest (blank_content::acc)
where where
-- Preserve the original document's line count. -- Preserve the original document's line count.
blank_content : String blank_content : String
blank_content = if elem '\n' (unpack x) blank_content = fastAppend (replicate (length (lines x)) "\n")
then concat $ replicate (length (lines x)) "\n"
else ""
reduce (MkToken _ _ (CodeLine m src) :: rest) acc = reduce (MkToken _ _ (CodeLine m src) :: rest) acc =
if m == trim src if m == trim src
then reduce rest (acc ++ "\n") then reduce rest ("\n"::acc)
else reduce rest (acc ++ (substr (length m + 1) -- remove space to right of marker. else reduce rest ((substr (length m + 1) -- remove space to right of marker.
(length src) (length src)
src)) src
)::acc)
reduce (MkToken _ _ (CodeBlock l r src) :: rest) acc with (lines src) -- Strip the deliminators surrounding the block. reduce (MkToken _ _ (CodeBlock l r src) :: rest) acc with (lines src) -- Strip the deliminators surrounding the block.
reduce (MkToken _ _ (CodeBlock l r src) :: rest) acc | [] = reduce rest acc -- 1 reduce (MkToken _ _ (CodeBlock l r src) :: rest) acc | [] = reduce rest acc -- 1
reduce (MkToken _ _ (CodeBlock l r src) :: rest) acc | (s :: ys) with (snocList ys) reduce (MkToken _ _ (CodeBlock l r src) :: rest) acc | (s :: ys) with (snocList ys)
reduce (MkToken _ _ (CodeBlock l r src) :: rest) acc | (s :: []) | Empty = reduce rest acc -- 2 reduce (MkToken _ _ (CodeBlock l r src) :: rest) acc | (s :: []) | Empty = reduce rest acc -- 2
reduce (MkToken _ _ (CodeBlock l r src) :: rest) acc | (s :: (srcs ++ [f])) | (Snoc f srcs rec) = reduce (MkToken _ _ (CodeBlock l r src) :: rest) acc | (s :: (srcs ++ [f])) | (Snoc f srcs rec) =
reduce rest (acc ++ "\n" ++ unlines srcs) reduce rest ("\n" :: unlines srcs :: acc)
-- [ NOTE ] 1 & 2 shouldn't happen as code blocks are well formed i.e. have two deliminators. -- [ NOTE ] 1 & 2 shouldn't happen as code blocks are well formed i.e. have two deliminators.
@ -164,7 +164,7 @@ extractCode : (specification : LiterateStyle)
-> Either LiterateError String -> Either LiterateError String
extractCode (MkLitStyle delims markers exts) str = extractCode (MkLitStyle delims markers exts) str =
case lex (rawTokens delims markers) str of case lex (rawTokens delims markers) str of
(toks, (_,_,"")) => Right $ reduce toks "" (toks, (_,_,"")) => Right (reduce toks Nil)
(_, (l,c,i)) => Left (MkLitErr l c i) (_, (l,c,i)) => Left (MkLitErr l c i)
||| Synonym for `extractCode`. ||| Synonym for `extractCode`.