hakyll-contrib-hyphenation 0.1.0.2 → 0.1.0.3
raw patch · 3 files changed
+69/−39 lines, 3 filesPVP: minor bump suggested
API additions: PVP suggests at least a minor version bump
API changes (from Hackage documentation)
+ Hakyll.Contrib.Hyphenation: hyphenateHtmlText :: Hyphenator -> String -> String
Files
- README.md +11/−0
- hakyll-contrib-hyphenation.cabal +9/−3
- src/Hakyll/Contrib/Hyphenation.hs +49/−36
+ README.md view
@@ -0,0 +1,11 @@+Automatic hyphenation for Hakyll+================================++This library provides [Hakyll][1] compiler to automatically hyphenate each word+in HTML output with Unicode [SOFT-HYPHEN][2] characters. It understands HTML+markup and doesn't try, for example, to hyphenate tag names and attributes. It+uses [hyphenation][3] package to do actual work of splitting words.++[1]: http://jaspervdj.be/hakyll+[2]: http://en.wikipedia.org/wiki/Soft_hyphen+[3]: http://hackage.haskell.org/package/hyphenation
hakyll-contrib-hyphenation.cabal view
@@ -1,7 +1,12 @@ name: hakyll-contrib-hyphenation-version: 0.1.0.2-synopsis: Automatic hyphenation for Hakyll--- description:+version: 0.1.0.3+synopsis: automatic hyphenation for Hakyll+description: This library provides Hakyll compiler to automatically+ hyphenate each word in HTML output with Unicode SOFT-HYPHEN+ characters. It understands HTML markup and doesn't try, for+ example, to hyphenate tag names and attributes. It uses+ <http://hackage.haskell.org/package/hyphenation hyphenation>+ package to do actual work of splitting words. category: Web stability: experimental author: Pavel Kretov <firegurafiku@gmail.com>@@ -13,6 +18,7 @@ build-type: Simple cabal-version: >= 1.6 tested-with: GHC == 7.6.3+extra-source-files: README.md library hs-source-dirs: src
src/Hakyll/Contrib/Hyphenation.hs view
@@ -8,36 +8,50 @@ -- they'll be shown only in case when line break actually occured, otherwise -- leaving them invisible. ----- Of course, this approach has its downsides you must be aware of, so use it--- with caution. Some of these downsides: increased size of generated HTML;--- more difficult text copying and pasing; the text may not even be displayd--- at all in some rare situations.--- -- Here is simple usage pattern for producing hyphenated texts, for example, -- English and Russian: ----- > import Hakyll.Contrib.Hyphenation (hyphenateHtml, english_US, russian)--- >--- > match "posts/*" $ do--- > route $ idRoute--- > compile $ pandocCompiler--- > >>= hyphenateHtml english_US--- > >>= hyphenateHtml russian--- > >>= loadAndApplyTemplate "..." ctx--- > >>= relativizeUrls+-- > import Hakyll.Contrib.Hyphenation (hyphenateHtml, english_US, russian)+-- >+-- > match "posts/*" $ do+-- > route $ setExtension "html"+-- > compile $ pandocCompiler+-- > >>= hyphenateHtml english_US+-- > >>= hyphenateHtml russian+-- > >>= loadAndApplyTemplate "..." ctx+-- > >>= relativizeUrls+--+-- Of course, this approach has its downsides you must be aware of, so use it+-- with caution. Some of these downsides:+--+-- * generated HTML code will slightly increase in size;+--+-- * soft hyphens will remain in text copied and pasted;+--+-- * the text may be displayed in a wrong way in some rare situations+-- (for example, in old Unicode-unaware browsers).+--+-- Taking these into account, you may better prefer to try+-- <http://www.w3.org/TR/css3-text/#hyphens CSS3 “hypens” property> instead, or+-- not to use text hyphenation at all. module Hakyll.Contrib.Hyphenation (+ module Text.Hyphenation.Language, hyphen, hyphenateText,- hyphenateHtml,- module Text.Hyphenation) where+ hyphenateHtmlText,+ hyphenateHtml) where -import Data.Char (isSpace)-import Data.List (intercalate)-import Data.List.Split (split, condense, whenElt)-import Text.Hyphenation-import Text.HTML.TagSoup-import Hakyll (Item, Compiler, itemBody, itemSetBody)+import Data.Char (isSpace)+import Data.List (intercalate)+import Data.List.Split (split, condense, whenElt)+import Text.Hyphenation (Hyphenator, hyphenate)+import Text.HTML.TagSoup (Tag(TagText), isTagText, fromTagText,+ parseTags, renderTags)+import Hakyll (Item, Compiler, itemBody, itemSetBody) +-- Module from 'hyphenate' package, re-exported for convenience of end-user.+import Text.Hyphenation.Language+ -- | Soft-hyphen character. -- This character indicates positions where word line can be safely broken and -- normally displayed only if word break occured otherwise being invisible.@@ -46,15 +60,15 @@ -- | Hyphenate every word of text. -- This function takes a hyphenator (usually a language definition from package--- 'Text.Hyphenation', for example 'english_US') and input string, then returns--- a new string with soft-hyphens inserted into each word. It does not preserve--- whitespaces and instead merges any number of consequent whitespaces into a--- single whitespace.+-- <http://hackage.haskell.com/package/hyphenation hyphenation>, for example+-- 'english_US') and input string, then returns a new string with soft-hyphens+-- inserted into each word. It does not preserve whitespaces and instead merges+-- any number of consequent whitespaces into a single whitespace. hyphenateText :: Hyphenator -> String -> String-hyphenateText lang = id- . concat- . map (\s -> intercalate hyphen $ hyphenate lang s)- . (split . condense . whenElt) isSpace+hyphenateText lang str = id+ $ concat+ $ map (\s -> intercalate hyphen $ hyphenate lang s)+ $ (split . condense . whenElt) isSpace str -- | Hyphenate every word of HTML-formatted text. -- This function takes a hyphenator and input HTML-formatted string, then@@ -63,19 +77,18 @@ -- any other special markup, instead only text content is affected. It does -- not also preserve original HTML code formatting. hyphenateHtmlText :: Hyphenator -> String -> String-hyphenateHtmlText lang = id- . renderTags- . map (\t -> if not $ isTagText t+hyphenateHtmlText lang str = id+ $ renderTags+ $ map (\t -> if not $ isTagText t then t else TagText $ hyphenateText lang $ fromTagText t )- . parseTags+ $ parseTags str -- | Hyphenate HTML body of Hakyll item. -- This is the convenience function for using in Hakyll rules as a part of -- compilers chain. Takes a hyphenator, then hyphenates item's body and returns -- a new item with updated body wrapped into 'Compiler' monad. hyphenateHtml :: Hyphenator -> Item String -> Compiler (Item String)-hyphenateHtml lang item = id- $ return+hyphenateHtml lang item = return $ (flip itemSetBody) item $ hyphenateHtmlText lang (itemBody item)