1
1
mirror of https://github.com/aelve/guide.git synced 2024-12-23 12:52:31 +03:00

Switch to the fmt library for formatting

This commit is contained in:
Artyom 2017-03-04 19:57:59 +03:00
parent e13a7fc832
commit 4ff43808ee
No known key found for this signature in database
GPG Key ID: B8E35A33FF522710
8 changed files with 43 additions and 32 deletions

View File

@ -94,6 +94,7 @@ library
, feed >= 0.3.11 && < 0.4 , feed >= 0.3.11 && < 0.4
, filemanip == 0.3.6.* , filemanip == 0.3.6.*
, filepath , filepath
, fmt == 0.0.0.4
, focus , focus
, friendly-time == 0.4.* , friendly-time == 0.4.*
, fsnotify == 0.2.* , fsnotify == 0.2.*

View File

@ -422,7 +422,7 @@ itemToFeedEntry baseUrl category item = do
Atom.entryContent = Just (Atom.HTMLContent (TL.unpack entryContent)) } Atom.entryContent = Just (Atom.HTMLContent (TL.unpack entryContent)) }
where where
entryLink = baseUrl // entryLink = baseUrl //
T.format "{}#item-{}" (categorySlug category, item^.uid) format "{}#item-{}" (categorySlug category, item^.uid)
entryBase = Atom.nullEntry entryBase = Atom.nullEntry
(T.unpack (uidToText (item^.uid))) (T.unpack (uidToText (item^.uid)))
(Atom.TextString (T.unpack (item^.name))) (Atom.TextString (T.unpack (item^.name)))

View File

@ -121,20 +121,22 @@ class JSFunction a where
-- This generates function definition -- This generates function definition
instance JSFunction JS where instance JSFunction JS where
makeJSFunction fName fParams fDef = makeJSFunction fName fParams fDef =
JS $ T.format "function {}({}) {\n{}}\n" let paramList = T.intercalate "," fParams
(fName, T.intercalate "," fParams, fDef) in JS $ "function "%<fName>%"("%<paramList>%") {\n"
%<indent 2 (build fDef)>%
"}\n"
-- This generates a function that takes arguments and produces a Javascript -- This generates a function that takes arguments and produces a Javascript
-- function call -- function call
instance JSParams a => JSFunction (a -> JS) where instance JSParams a => JSFunction (a -> JS) where
makeJSFunction fName _fParams _fDef = \args -> makeJSFunction fName _fParams _fDef = \args ->
JS $ T.format "{}({});" let paramList = T.intercalate "," (map fromJS (jsParams args))
(fName, T.intercalate "," (map fromJS (jsParams args))) in JS $ ""%<fName>%"("%<paramList>%");"
-- This isn't a standalone function and so it doesn't have to be listed in -- This isn't a standalone function and so it doesn't have to be listed in
-- 'allJSFunctions'. -- 'allJSFunctions'.
assign :: ToJS x => JS -> x -> JS assign :: ToJS x => JS -> x -> JS
assign v x = JS $ T.format "{} = {};" (v, toJS x) assign v x = JS $ format "{} = {};" (v, toJS x)
-- TODO: all links here shouldn't be absolute [absolute-links] -- TODO: all links here shouldn't be absolute [absolute-links]
@ -708,19 +710,20 @@ newtype JQuerySelector = JQuerySelector Text
deriving (ToJS, T.Buildable) deriving (ToJS, T.Buildable)
selectId :: Text -> JQuerySelector selectId :: Text -> JQuerySelector
selectId x = JQuerySelector $ T.format "#{}" [x] selectId x = JQuerySelector $ format "#{}" [x]
selectUid :: Uid Node -> JQuerySelector selectUid :: Uid Node -> JQuerySelector
selectUid x = JQuerySelector $ T.format "#{}" [x] selectUid x = JQuerySelector $ format "#{}" [x]
selectClass :: Text -> JQuerySelector selectClass :: Text -> JQuerySelector
selectClass x = JQuerySelector $ T.format ".{}" [x] selectClass x = JQuerySelector $ format ".{}" [x]
selectParent :: JQuerySelector -> JQuerySelector selectParent :: JQuerySelector -> JQuerySelector
selectParent x = JQuerySelector $ T.format ":has(> {})" [x] selectParent x = JQuerySelector $ format ":has(> {})" [x]
selectChildren :: JQuerySelector -> JQuerySelector -> JQuerySelector selectChildren :: JQuerySelector -> JQuerySelector -> JQuerySelector
selectChildren a b = JQuerySelector $ T.format "{} > {}" (a, b) selectChildren a b = JQuerySelector $ format "{} > {}" (a, b)
selectSection :: JQuerySelector -> Text -> JQuerySelector selectSection :: JQuerySelector -> Text -> JQuerySelector
selectSection a b = JQuerySelector $ T.format "{} > .section.{}" (a, b) selectSection a b = JQuerySelector $ format "{} > .section.{}" (a, b)

View File

@ -260,7 +260,7 @@ instance A.ToJSON Category where
categorySlug :: Category -> Text categorySlug :: Category -> Text
categorySlug category = categorySlug category =
T.format "{}-{}" (makeSlug (category^.title), category^.uid) format "{}-{}" (makeSlug (category^.title), category^.uid)
---------------------------------------------------------------------------- ----------------------------------------------------------------------------
-- Utils -- Utils

View File

@ -1,4 +1,5 @@
{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
@ -311,8 +312,8 @@ renderEdits globalState edits = do
-- Unlike 'groupWith', “groupBy . equating” doesn't sort the input. -- Unlike 'groupWith', “groupBy . equating” doesn't sort the input.
let editBlocks = groupBy (equating getIP) edits let editBlocks = groupBy (equating getIP) edits
let ipNum = length $ groupWith getIP edits let ipNum = length $ groupWith getIP edits
h1_ $ toHtml $ h1_ $ toHtml @Text $
T.format "Pending edits (IPs: {}, blocks: {})" (ipNum, length editBlocks) "Pending edits (IPs: "%<ipNum>%", blocks: "%<length editBlocks>%")"
for_ editBlocks $ \editBlock -> div_ $ do for_ editBlocks $ \editBlock -> div_ $ do
blockNode <- thisNode blockNode <- thisNode
h2_ $ do h2_ $ do

View File

@ -95,7 +95,8 @@ import Guide.Markdown
-- | Add a script that does something on page load. -- | Add a script that does something on page load.
onPageLoad :: Monad m => JS -> HtmlT m () onPageLoad :: Monad m => JS -> HtmlT m ()
onPageLoad js = script_ $ T.format "$(document).ready(function(){{}});" [js] onPageLoad js = script_ $
"$(document).ready(function(){"%<js>%"});"
-- | Add some empty space. -- | Add some empty space.
emptySpan :: Monad m => Text -> HtmlT m () emptySpan :: Monad m => Text -> HtmlT m ()
@ -104,19 +105,19 @@ emptySpan w = span_ [style_ ("margin-left:" <> w)] mempty
-- Use inputValue to get the value (works with input_ and textarea_) -- Use inputValue to get the value (works with input_ and textarea_)
onEnter :: JS -> Attribute onEnter :: JS -> Attribute
onEnter handler = onkeydown_ $ onEnter handler = onkeydown_ $
T.format "if (event.keyCode == 13 || event.keyCode == 10)\ "if (event.keyCode == 13 || event.keyCode == 10) {"
\ {{} return false;}\n" [handler] %<handler>%" return false;}\n"
onCtrlEnter :: JS -> Attribute onCtrlEnter :: JS -> Attribute
onCtrlEnter handler = onkeydown_ $ onCtrlEnter handler = onkeydown_ $
T.format "if ((event.keyCode == 13 || event.keyCode == 10) &&\ "if ((event.keyCode == 13 || event.keyCode == 10) && " <>
\ (event.metaKey || event.ctrlKey))\ "(event.metaKey || event.ctrlKey)) {"
\ {{} return false;}\n" [handler] %<handler>%" return false;}\n"
onEscape :: JS -> Attribute onEscape :: JS -> Attribute
onEscape handler = onkeydown_ $ onEscape handler = onkeydown_ $
T.format "if (event.keyCode == 27)\ "if (event.keyCode == 27) {"
\ {{} return false;}\n" [handler] %<handler>%" return false;}\n"
textInput :: Monad m => [Attribute] -> HtmlT m () textInput :: Monad m => [Attribute] -> HtmlT m ()
textInput attrs = input_ (type_ "text" : attrs) textInput attrs = input_ (type_ "text" : attrs)
@ -128,7 +129,7 @@ clearInput :: JS
clearInput = JS "this.value = '';" clearInput = JS "this.value = '';"
onFormSubmit :: (JS -> JS) -> Attribute onFormSubmit :: (JS -> JS) -> Attribute
onFormSubmit f = onsubmit_ $ T.format "{} return false;" [f (JS "this")] onFormSubmit f = onsubmit_ $ format "{} return false;" [f (JS "this")]
button :: Monad m => Text -> [Attribute] -> JS -> HtmlT m () button :: Monad m => Text -> [Attribute] -> JS -> HtmlT m ()
button value attrs handler = button value attrs handler =
@ -177,7 +178,7 @@ markdownEditor
-> HtmlT m () -> HtmlT m ()
markdownEditor attr (view mdText -> s) submit cancel instr = do markdownEditor attr (view mdText -> s) submit cancel instr = do
textareaUid <- randomLongUid textareaUid <- randomLongUid
let val = JS $ T.format "document.getElementById(\"{}\").value" [textareaUid] let val = JS $ "document.getElementById(\""%<textareaUid>%"\").value"
-- Autocomplete has to be turned off thanks to -- Autocomplete has to be turned off thanks to
-- <http://stackoverflow.com/q/8311455>. -- <http://stackoverflow.com/q/8311455>.
textarea_ ([uid_ textareaUid, textarea_ ([uid_ textareaUid,
@ -196,7 +197,8 @@ markdownEditor attr (view mdText -> s) submit cancel instr = do
emptySpan "6px" emptySpan "6px"
span_ [class_ "edit-field-instruction"] (toHtml instr) span_ [class_ "edit-field-instruction"] (toHtml instr)
a_ [href_ "/markdown", target_ "_blank"] $ a_ [href_ "/markdown", target_ "_blank"] $
img_ [src_ "/markdown.svg", alt_ "markdown supported", class_ " markdown-supported "] img_ [src_ "/markdown.svg", alt_ "markdown supported",
class_ " markdown-supported "]
smallMarkdownEditor smallMarkdownEditor
:: MonadIO m :: MonadIO m
@ -208,7 +210,7 @@ smallMarkdownEditor
-> HtmlT m () -> HtmlT m ()
smallMarkdownEditor attr (view mdText -> s) submit mbCancel instr = do smallMarkdownEditor attr (view mdText -> s) submit mbCancel instr = do
textareaId <- randomLongUid textareaId <- randomLongUid
let val = JS $ T.format "document.getElementById(\"{}\").value" [textareaId] let val = JS $ "document.getElementById(\""%<textareaId>%"\").value"
textarea_ ([class_ "fullwidth", uid_ textareaId, autocomplete_ "off"] ++ textarea_ ([class_ "fullwidth", uid_ textareaId, autocomplete_ "off"] ++
[onEnter (submit val)] ++ [onEnter (submit val)] ++
[onEscape cancel | Just cancel <- [mbCancel]] ++ [onEscape cancel | Just cancel <- [mbCancel]] ++
@ -222,7 +224,8 @@ smallMarkdownEditor attr (view mdText -> s) submit mbCancel instr = do
span_ [style_ "float:right"] $ do span_ [style_ "float:right"] $ do
span_ [class_ "edit-field-instruction"] (toHtml instr) span_ [class_ "edit-field-instruction"] (toHtml instr)
a_ [href_ "/markdown", target_ "_blank"] $ a_ [href_ "/markdown", target_ "_blank"] $
img_ [src_ "/markdown.svg", alt_ "markdown supported", class_ " markdown-supported "] img_ [src_ "/markdown.svg", alt_ "markdown supported",
class_ " markdown-supported "]
thisNode :: MonadIO m => HtmlT m JQuerySelector thisNode :: MonadIO m => HtmlT m JQuerySelector
thisNode = do thisNode = do
@ -233,18 +236,18 @@ thisNode = do
return (JS.selectParent (JS.selectUid uid')) return (JS.selectParent (JS.selectUid uid'))
itemNodeId :: Item -> Text itemNodeId :: Item -> Text
itemNodeId item = "item-" <> uidToText (item^.uid) itemNodeId item = format "item-{}" [item^.uid]
categoryNodeId :: Category -> Text categoryNodeId :: Category -> Text
categoryNodeId category = "category-" <> uidToText (category^.uid) categoryNodeId category = format "category-{}" [category^.uid]
-- TODO: another absolute link to get rid of [absolute-links] -- TODO: another absolute link to get rid of [absolute-links]
categoryLink :: Category -> Url categoryLink :: Category -> Url
categoryLink category = "/haskell/" <> categorySlug category categoryLink category = format "/haskell/{}" [categorySlug category]
itemLink :: Category -> Item -> Url itemLink :: Category -> Item -> Url
itemLink category item = itemLink category item =
T.format "/haskell/{}#{}" (categorySlug category, itemNodeId item) format "/haskell/{}#{}" (categorySlug category, itemNodeId item)
-- See Note [show-hide]; wheh changing these, also look at 'JS.switchSection'. -- See Note [show-hide]; wheh changing these, also look at 'JS.switchSection'.
shown, noScriptShown :: Attribute shown, noScriptShown :: Attribute

View File

@ -38,6 +38,8 @@ import Control.DeepSeq as X
import Data.Hashable as X import Data.Hashable as X
-- Lazy bytestring -- Lazy bytestring
import qualified Data.ByteString.Lazy as BSL import qualified Data.ByteString.Lazy as BSL
-- Formatting
import Fmt as X
type LByteString = BSL.ByteString type LByteString = BSL.ByteString

View File

@ -12,3 +12,4 @@ extra-deps:
- http-client-0.5.1 - http-client-0.5.1
- edit-distance-vector-1.0.0.4 - edit-distance-vector-1.0.0.4
- patches-vector-0.1.5.4 - patches-vector-0.1.5.4
- fmt-0.0.0.4