Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added doctests, vim tag support, moved content to src, hit 100% haddo…

…ck coverage
  • Loading branch information...
commit 391772c7251109bcc86e28949ad2d91f0e27b167 1 parent f0e715e
@ekmett authored
View
1  .ghci
@@ -0,0 +1 @@
+:set -isrc -idist/build/autogen -optP-include -optPdist/build/autogen/cabal_macros.h
View
2  .gitignore
@@ -1 +1,3 @@
dist
+tags
+TAGS
View
21 .vim.custom
@@ -0,0 +1,21 @@
+" Add the following to your .vimrc to automatically load this on startup
+" if filereadable(".vim.custom")
+" so .vim.custom
+" endif
+
+function StripTrailingWhitespace()
+ let myline=line(".")
+ let mycolumn = col(".")
+ silent %s/ *$//
+ call cursor(myline, mycolumn)
+endfunction
+
+syntax on
+set tags=TAGS;/
+set listchars=tab:‗‗,trail:‗
+set list
+
+map <F2> :exec ":!hasktags -x -c --ignore src"<CR><CR>
+
+au BufWritePre *.hs,*.markdown silent! cal StripTrailingWhitespace()
+au BufWritePost *.hs silent! :exec ":!hasktags -x -c --ignore src"
View
30 hyphenation.cabal
@@ -1,8 +1,8 @@
name: hyphenation
category: Text
-version: 0.2.1.2
+version: 0.2.1.3
license: BSD3
-cabal-version: >= 1.6
+cabal-version: >= 1.8
license-file: LICENSE
author: Edward A. Kmett
maintainer: Edward A. Kmett <ekmett@gmail.com>
@@ -20,14 +20,14 @@ description:
.
Usage:
.
- > ghci> hyphenate english_US "supercalifragilisticexpialadocious"
- > ["su","per","cal","ifrag","ilis","tic","ex","pi","al","ado","cious"]
+ >>> hyphenate english_US "supercalifragilisticexpialadocious"
+ ["su","per","cal","ifrag","ilis","tic","ex","pi","al","ado","cious"]
.
- > ghci> hyphenate english_US "hyphenation"
- > ["hy","phen","ation"]
+ >>> hyphenate english_US "hyphenation"
+ ["hy","phen","ation"]
.
- > ghci> hyphenate icelandic "vaðlaheiðavegavinnuverkfærageymsluskúr"
- > ["va\240la","hei\240a","vega","vinnu","verk","f\230ra","geymslu","sk\250r"]
+ >>> hyphenate icelandic "vaðlaheiðavegavinnuverkfærageymsluskúr"
+ ["va\240la","hei\240a","vega","vinnu","verk","f\230ra","geymslu","sk\250r"]
build-type: Simple
data-dir: data
@@ -48,11 +48,23 @@ library
Text.Hyphenation
Text.Hyphenation.Hyphenator
Text.Hyphenation.Language
-
Text.Hyphenation.Exception
Text.Hyphenation.Pattern
other-modules:
Paths_hyphenation
+ hs-source-dirs: src
ghc-options: -Wall
+
+-- Verify the results of the examples
+test-suite doctests
+ type: exitcode-stdio-1.0
+ main-is: doctests.hs
+ build-depends:
+ base == 4.*,
+ directory >= 1.0 && < 1.2,
+ doctest >= 0.8 && <= 0.9,
+ filepath >= 1.3 && < 1.4
+ ghc-options: -Wall -Werror -threaded
+ hs-source-dirs: tests
View
0  Text/Hyphenation.hs → src/Text/Hyphenation.hs
File renamed without changes
View
6 Text/Hyphenation/Exception.hs → src/Text/Hyphenation/Exception.hs
@@ -23,7 +23,7 @@ import qualified Data.HashMap.Strict as HM
import Data.Monoid
import Prelude hiding (lookup)
--- manually supplied hyphenations
+-- | Hyphenation exceptions are special cases that should use the specified hyphenation points.
newtype Exceptions = Exceptions (HM.HashMap String [Int])
deriving Show
@@ -44,15 +44,17 @@ addException :: String -> Exceptions -> Exceptions
addException s (Exceptions m) = Exceptions $
HM.insertWith zipMin (filter (/= '-') s) (scoreException s) m
+-- | Try to find a matching hyphenation exception.
lookupException :: String -> Exceptions -> Maybe [Int]
lookupException s (Exceptions m) = HM.lookup s m
+-- | Convert an exception string to a score.
scoreException :: String -> [Int]
scoreException [] = [0]
scoreException (x:ys)
| x == '-' = 1 : if null ys then [] else scoreException (tail ys)
| otherwise = 0 : scoreException ys
--- parse one exception per line from an input string
+-- | Parse one exception per line from an input string
parseExceptions :: String -> Exceptions
parseExceptions = foldr addException mempty . lines
View
27 Text/Hyphenation/Hyphenator.hs → src/Text/Hyphenation/Hyphenator.hs
@@ -21,10 +21,21 @@ module Text.Hyphenation.Hyphenator
import Text.Hyphenation.Pattern
import Text.Hyphenation.Exception
-defaultLeftMin, defaultRightMin :: Int
+-- | By default, do not insert hyphens in the first two characters
+--
+-- >>> defaultLeftMin
+-- 2
+defaultLeftMin :: Int
defaultLeftMin = 2
+-- | By default, do not insert hyphens in the last three characters.
+--
+-- >>> defaultRightMin
+-- 3
+defaultRightMin :: Int
defaultRightMin = 3
+-- | A @Hyphenator@ is combination of an alphabet normalization scheme, a set of 'Patterns', a set of 'Exceptions' to those patterns
+-- and a number of characters at each end to skip hyphenating.
data Hyphenator = Hyphenator
{ hyphenatorChars :: Char -> Char -- ^ a normalization function applied to input characters before applying patterns or exceptions
, hyphenatorPatterns :: Patterns -- ^ hyphenation patterns stored in a trie
@@ -33,6 +44,7 @@ data Hyphenator = Hyphenator
, hyphenatorRightMin :: {-# UNPACK #-} !Int -- ^ the number of characters at the end of the word to skip hyphenating, by default: 3
}
+-- | Using a 'Hyphenator', compute the score of a string.
hyphenationScore :: Hyphenator -> String -> [Int]
hyphenationScore (Hyphenator nf ps es l r) s
| l + r >= n = replicate (n + 1) 0
@@ -46,11 +58,14 @@ hyphenationScore (Hyphenator nf ps es l r) s
-- | hyphenate a single word using the specified Hyphenator. Returns a set of candidate breakpoints by decomposing the input
-- into substrings.
---
--- > ghci> hyphenate english_US "supercalifragilisticexpialadocious"
--- > ["su","per","cal","ifrag","ilis","tic","ex","pi","al","ado","cious"]
--- > ghci> hyphenate english_US "hyphenation"
--- > ["hy","phen","ation"]
+--
+-- >>> import Text.Hyphenation
+--
+-- >>> hyphenate english_US "supercalifragilisticexpialadocious"
+-- ["su","per","cal","ifrag","ilis","tic","ex","pi","al","ado","cious"]
+--
+-- >>> hyphenate english_US "hyphenation"
+-- ["hy","phen","ation"]
hyphenate :: Hyphenator -> String -> [String]
hyphenate h s0 = go [] s0 $ tail $ hyphenationScore h s0 where
go acc (w:ws) (p:ps)
View
20 Text/Hyphenation/Language.hs → src/Text/Hyphenation/Language.hs
@@ -55,6 +55,7 @@ loadHyphenator language = do
tryLookup x = fromMaybe x $ IM.lookup (fromEnum x) chrMap
return $ Hyphenator tryLookup (parsePatterns pat) (parseExceptions hyp) defaultLeftMin defaultRightMin
+-- | A strongly typed set of available languages you can use for hyphenation.
data Language
= Afrikaans
| Basque
@@ -192,30 +193,30 @@ languageAffix s = case s of
-- |
--- > ghci> hyphenate english_US "supercalifragilisticexpialadocious"
--- > ["su","per","cal","ifrag","ilis","tic","ex","pi","al","ado","cious"]
+-- >>> hyphenate english_US "supercalifragilisticexpialadocious"
+-- ["su","per","cal","ifrag","ilis","tic","ex","pi","al","ado","cious"]
--
-- favors US hyphenation
english_US :: Hyphenator
-- |
--- > ghci> hyphenate english_GB "supercalifragilisticexpialadocious"
--- > ["su","per","cal","ifrag","ilis","tic","ex","pi","al","ado","cious"]
+-- >>> hyphenate english_GB "supercalifragilisticexpialadocious"
+-- ["su","per","cal","i","fra","gil","istic","ex","pi","alado","cious"]
--
-- favors UK hyphenation
english_GB :: Hyphenator
-- |
--- > ghci> hyphenate french "anticonstitutionnellement"
--- > ["an","ti","cons","ti","tu","tion","nel","le","ment"]
+-- >>> hyphenate french "anticonstitutionnellement"
+-- ["an","ti","cons","ti","tu","tion","nel","le","ment"]
french :: Hyphenator
-- |
--- > ghci> hyphenate icelandic "vaðlaheiðavegavinnuverkfærageymsluskúr"
--- > ["va\240la","hei\240a","vega","vinnu","verk","f\230ra","geymslu","sk\250r"]
+-- >>> hyphenate icelandic "vaðlaheiðavegavinnuverkfærageymsluskúr"
+-- ["va\240la","hei\240a","vega","vinnu","verk","f\230ra","geymslu","sk\250r"]
icelandic :: Hyphenator
-
+-- | Hyphenators for a wide array of languages.
afrikaans, basque, bengali, bulgarian, catalan, chinese,
coptic, croatian, czech, danish, dutch, esperanto,
estonian, ethiopic, farsi, finnish, galician, german_1901, german_1996,
@@ -294,6 +295,7 @@ ukrainian = unsafePerformIO (loadHyphenator (languageAffix Ukrainian))
uppersorbian = unsafePerformIO (loadHyphenator (languageAffix Uppersorbian))
welsh = unsafePerformIO (loadHyphenator (languageAffix Welsh))
+-- | Load (and cache) the hyphenator for a given language.
languageHyphenator :: Language -> Hyphenator
languageHyphenator s = case s of
Afrikaans -> afrikaans
View
3  Text/Hyphenation/Pattern.hs → src/Text/Hyphenation/Pattern.hs
@@ -24,6 +24,7 @@ import Data.Monoid
import Prelude hiding (lookup)
import Data.Char (digitToInt, isDigit)
+-- | Hyphenation patterns
data Patterns = Patterns [Int] (IM.IntMap Patterns)
deriving Show
@@ -68,12 +69,14 @@ parsePatterns = foldr insertPattern mempty . lines
chars :: String -> String
chars = filter (\x -> (x < '0' || x > '9'))
+-- | Convert a Pattern string to a Score
scorePattern :: String -> [Int]
scorePattern [] = [0]
scorePattern (x:ys)
| isDigit x = digitToInt x : if null ys then [] else scorePattern (tail ys)
| otherwise = 0 : scorePattern ys
+-- | Zip two scores.
zipMax :: [Int] -> [Int] -> [Int]
zipMax (x:xs) (y:ys) = max x y : zipMax xs ys
zipMax [] ys = ys
View
28 tests/doctests.hs
@@ -0,0 +1,28 @@
+module Main where
+
+import Test.DocTest
+import System.Directory
+import System.FilePath
+import Control.Applicative
+import Control.Monad
+import Data.List
+
+main :: IO ()
+main = getSources >>= \sources -> doctest $
+ "-isrc"
+ : "-idist/build/autogen"
+ : "-optP-include"
+ : "-optPdist/build/autogen/cabal_macros.h"
+ : sources
+
+getSources :: IO [FilePath]
+getSources = filter (isSuffixOf ".hs") <$> go "src"
+ where
+ go dir = do
+ (dirs, files) <- getFilesAndDirectories dir
+ (files ++) . concat <$> mapM go dirs
+
+getFilesAndDirectories :: FilePath -> IO ([FilePath], [FilePath])
+getFilesAndDirectories dir = do
+ c <- map (dir </>) . filter (`notElem` ["..", "."]) <$> getDirectoryContents dir
+ (,) <$> filterM doesDirectoryExist c <*> filterM doesFileExist c
Please sign in to comment.
Something went wrong with that request. Please try again.