Created
June 21, 2016 13:13
-
-
Save freakingawesome/2754fdc1ed15356074dc09e34962819b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Html exposing (..) | |
import Html.Attributes exposing (..) | |
import Combine exposing (..) | |
import Combine.Char exposing (..) | |
import Combine.Infix exposing (..) | |
import String | |
import List.Extra exposing (groupWhile) | |
testInput = | |
"hello (!BOLD!) I Am a bold text (!BOLD!)bla bla la" | |
main = | |
case parse htmlParser testInput of | |
(Ok htmls, _) -> div [] htmls | |
(Err err, _) -> div [ style [("color", "red")] ] [ text <| toString <| err] | |
type Style | |
= Unstyled | |
| Bold | |
htmlParser : Parser (List (Html msg)) | |
htmlParser = | |
styleParser False | |
`andThen` (succeed << foldStyledHtml) | |
foldStyledHtml : List (Char, Style) -> List (Html msg) | |
foldStyledHtml chars = | |
let | |
foldSingleStyledHtml = | |
List.foldr (\(c, s) (cs, _) -> (c :: cs, s)) ([], Unstyled) | |
>> \(chars, style) -> | |
let str = String.fromList chars | |
in case style of | |
Unstyled -> text str | |
Bold -> b [] [ text str ] | |
in | |
groupWhile (\a b -> snd a == snd b) chars | |
|> List.map foldSingleStyledHtml | |
styleParser : Bool -> Parser (List (Char, Style)) | |
styleParser bolded = | |
let | |
style = if bolded then Bold else Unstyled | |
in | |
(end `andThen` always (succeed [])) | |
<|> (string "(!BOLD!)" `andThen` \_ -> styleParser (not bolded)) | |
<|> (anyChar | |
`andThen` \c -> styleParser bolded | |
`andThen` \cs -> (succeed ((c, style) :: cs))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment