From 514be925c24f213791b92a2e2ddce6b04688f09a Mon Sep 17 00:00:00 2001 From: VladimirLogachev Date: Sat, 4 May 2024 21:29:22 +0400 Subject: [PATCH] Add Typography module --- README.md | 28 +++++----- src/Typography.elm | 125 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 12 deletions(-) create mode 100644 src/Typography.elm diff --git a/README.md b/README.md index 2e2009f..bf53f09 100644 --- a/README.md +++ b/README.md @@ -6,26 +6,30 @@ Heavily borrows from [culori](https://culorijs.org/). ## Release Plan: -- rename repo - - Modules: - Typography + - review code + - write docstrings + - check typos with grammarly + - demonstrate in the example app + - any tests - InlineStyle + - review code + - write docstrings + - check typos with grammarly + - demonstrate in the example app + - any tests - ExtraColor + - review code + - write docstrings + - check typos with grammarly + - demonstrate in the example app - add cool palettes - deal with negative css values, display the output - maybe add Element.Color to the ADT? - definite write a lot of test cases, and render the string output - Culori (not exposed) + - review code + - consider splitting to modules as in the original - Find more reusable code in the old apps - -- Each Module: - - - review code - - write docstrings - - check typos with grammarly - - demonstrate in the example app - - any tests - -- Module diff --git a/src/Typography.elm b/src/Typography.elm new file mode 100644 index 0000000..f144120 --- /dev/null +++ b/src/Typography.elm @@ -0,0 +1,125 @@ +module Typography exposing (nbsp, preparedText) + +import Element exposing (..) +import Set exposing (Set) + + +{-| A helper to avoid typographic mistakes. + +1. Split string into lines +2. Glue last 2 words in a line together (if line consists of 3 and more) +3. Replace spaces after specific words with nbsp +4. Glue lines back together + +Note: Ideally it should be stored in both clean and processed forms in database. +Note: We may even come up with a separate elm package, carefully ported from js. + +-} +preparedText : String -> Element msg +preparedText x = + String.lines x + |> List.map processLine + |> String.join "\n" + |> text + + +processLine : String -> String +processLine = + String.words + >> List.foldr + (\word tail -> + if Set.member (String.toLower word) dictionary then + word ++ nbsp ++ tail + + else if tail == "" then + word + + else + word ++ " " ++ tail + ) + "" + + +nbsp : String +nbsp = + "\u{00A0}" + + +dictionary : Set String +dictionary = + [ -- punctuation + "-" + , "—" + , "+" + + -- english + , "a" + , "about" + , "an" + , "and" + , "any" + , "are" + , "as" + , "at" + , "au" + , "be" + , "bi" + , "but" + , "by" + , "can" + , "de" + , "do" + , "et" + , "fit" + , "for" + , "from" + , "give" + , "go" + , "going" + , "had" + , "he" + , "i" + , "if" + , "in" + , "is" + , "it" + , "just" + , "know" + , "la" + , "let" + , "made" + , "may" + , "me" + , "my" + , "no" + , "not" + , "of" + , "on" + , "or" + , "part" + , "real" + , "see" + , "seek" + , "sent" + , "so" + , "than" + , "that" + , "the" + , "them" + , "there" + , "this" + , "to" + , "up" + , "upon" + , "watch" + , "wd" + , "we" + , "what" + , "whether" + , "which" + , "who" + , "why" + , "will" + , "with" + ] + |> Set.fromList