hyakko 0.3.2 → 0.4.0
raw patch · 4 files changed
+61/−52 lines, 4 filesdep +text
Dependencies added: text
Files
- LICENSE +1/−1
- hyakko.cabal +7/−6
- src/Hyakko.hs +51/−43
- src/Text/Markdown.hs +2/−2
LICENSE view
@@ -1,4 +1,4 @@-Copyright (c) 2011 Jeremy Hull+Copyright (c) 2013 Jeremy Hull Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to
hyakko.cabal view
@@ -1,10 +1,10 @@ name: hyakko-version: 0.3.2+version: 0.4.0 cabal-version: >= 1.6 build-type: Simple license: MIT license-file: LICENSE-copyright: (c) 2011 Jeremy Hull+copyright: (c) 2013 Jeremy Hull author: Jeremy Hull <sourdrums@gmail.com> maintainer: Jeremy Hull <sourdrums@gmail.com> bug-reports: https://github.com/sourrust/hyakko/issues@@ -13,9 +13,9 @@ category: Documentation tested-with: GHC == 7.4.2 synopsis: Literate-style Documentation Generator-description: Hyakko is a Haskell port of Docco: the original quick-and-dirty,- hundred-line-long, literate-programming-style documentation- generator.+description: Hyakko is a Haskell port of Docco: the original+ quick-and-dirty, hundred-line-long,+ literate-programming-style documentation generator. data-files: -- HTML template and CSS sytle resources/hyakko.html@@ -40,7 +40,8 @@ containers >= 0.4, directory >= 1, pandoc >= 1.10,- bytestring >= 0.9+ bytestring >= 0.9,+ text >= 0.11 hs-source-dirs: src ghc-options: -O2 -Wall main-is: Hyakko.hs
src/Hyakko.hs view
@@ -34,6 +34,9 @@ import qualified Data.Map as M import Data.ByteString.Lazy.Char8 (ByteString) import qualified Data.ByteString.Lazy.Char8 as L+import Data.Text (Text)+import qualified Data.Text as T+import qualified Data.Text.IO as T import Data.List (sort, groupBy, genericIndex) import Data.Maybe (fromJust) import Control.Monad (filterM, (>=>), forM)@@ -56,17 +59,21 @@ -- ### Main Documentation Generation Functions -(><) :: ByteString -> ByteString -> ByteString-(><) = L.append-{-# INLINE (><) #-}+(++.) :: Text -> Text -> Text+(++.) = T.append+{-# INLINE (++.) #-} +(++*) :: ByteString -> ByteString -> ByteString+(++*) = L.append+{-# INLINE (++*) #-}+ -- Generate the documentation for a source file by reading it in, splitting -- it up into comment/code sections, highlighting them for the appropriate -- language, and merging them into an HTML template. generateDocumentation :: [FilePath] -> IO () generateDocumentation [] = return () generateDocumentation (x:xs) = do- code <- L.readFile x+ code <- T.readFile x let sections = parse (getLanguage x) code if null sections then putStrLn $ "hyakko doesn't support the language extension " ++ takeExtension x@@ -87,12 +94,12 @@ -- ("codeHtml", ...) -- ] ---inSections :: [ByteString] -> ByteString -> [Map String ByteString]+inSections :: [Text] -> ByteString -> [Map String Text] inSections xs r = [M.fromList l | l <- clump sections] where -- Bring the lists together into groups of comment and groups of code -- pattern.- sections :: [[ByteString]]+ sections :: [[Text]] sections = ensurePair . map concat -- Group code into a list . groupBy' head not@@ -100,37 +107,38 @@ $ groupBy' id id xs -- Clump sectioned off lines into doc and code text.- clump :: [[ByteString]] -> [[(String, ByteString)]]+ clump :: [[Text]] -> [[(String, Text)]] clump [x] = clump $ ensurePair [x] clump (x:y:ys) = [ ("docsText", replace x)- , ("codeText", L.unlines y)+ , ("codeText", T.unlines y) ] : clump ys clump _ = [] -- Generalized function used to section off code and comments groupBy' t t1 = groupBy $ \x y ->- and $ map (t1 . (=~ r)) [t x, t y]+ and $ map (t1 . (=~ r) . T.unpack) [t x, t y] -- Replace the beggining comment symbol with nothing- replace :: [ByteString] -> ByteString- replace = L.unlines . map (\x ->- let y = L.unpack x+ replace :: [Text] -> Text+ replace = T.unlines . map (\x ->+ let y = T.unpack x mkReg = mkRegex . (=~ r)- in L.pack $ subRegex (mkReg y) y "")+ in T.pack $ subRegex (mkReg y) y "") -- Make sure the result is in the right pairing order- ensurePair :: [[ByteString]] -> [[ByteString]]+ ensurePair :: [[Text]] -> [[Text]] ensurePair ys | even (length ys) = ys | otherwise = appendList [[""]]- where appendList | (head . head) ys =~ r = (ys ++)- | otherwise = (++ ys)+ where appendList | toBytes ys =~ r = (ys ++)+ | otherwise = (++ ys) -parse :: Maybe (Map String ByteString)- -> ByteString- -> [Map String ByteString]+ toBytes :: [[Text]] -> ByteString+ toBytes = L.pack . T.unpack . head . head++parse :: Maybe (Map String ByteString) -> Text -> [Map String Text] parse Nothing _ = [] parse (Just src) code = inSections line $ src M.! "comment"- where line = filter ((/=) "#!" . L.take 2) $ L.lines code+ where line = filter ((/=) "#!" . T.take 2) $ T.lines code -- Highlights a single chunk of Haskell code, using **Pygments** over stdio, -- and runs the text of its corresponding comment through **Markdown**,@@ -141,14 +149,14 @@ -- little marker comments between each section and then splitting the result -- string wherever our markers occur. highlight :: FilePath- -> [Map String ByteString]+ -> [Map String Text] -> IO (String, Map String ByteString) highlight src section = do let language = fromJust $ getLanguage src options = ["-l", L.unpack $ language M.! "name", "-f", "html", "-O", "encoding=utf-8"] input = concatMap (\x ->- let codeText = L.unpack $ x M.! "codeText"+ let codeText = T.unpack $ x M.! "codeText" divider = L.unpack $ language M.! "dividerText" in codeText ++ divider) section @@ -159,17 +167,17 @@ -- After `highlight` is called, there are divider inside to show when the -- hightlighed stop and code begins. `mapSections` is used to take out the -- dividers and put them into `docsHtml` and `codeHtml` sections.-mapSections :: [Map String ByteString]+mapSections :: [Map String Text] -> String -> Map String ByteString- -> [Map String ByteString]+ -> [Map String Text] mapSections section highlighted language = let output = subRegex (mkRegex highlightReplace) highlighted "" divider = mkRegex . L.unpack $ language M.! "dividerHtml" fragments = splitRegex divider output- packFrag = L.pack . genericIndex fragments- docText s = toHTML . L.unpack $ s M.! "docsText"- codeText i = highlightStart >< packFrag i >< highlightEnd+ packFrag = T.pack . genericIndex fragments+ docText s = toHTML . T.unpack $ s M.! "docsText"+ codeText i = highlightStart ++. packFrag i ++. highlightEnd sectLength = (length section) - 1 intoMap x = let sect = section !! x in M.insert "docsHtml" (docText sect) $@@ -199,7 +207,7 @@ -- <tr id="section-$number$"> -- <td class="docs"> -- <div class="pilwrap">--- <a class="pilcrow" href="#section-$number$">λ</a>+-- <a class="pilcrow" href="#section-$number$">λ</a> -- </div> -- $doc-html$ -- </td>@@ -207,7 +215,7 @@ -- $code-html$ -- </td> -- </tr>-sectionTemplate :: [Map String ByteString]+sectionTemplate :: [Map String Text] -> [Int] -> [(String, String)] sectionTemplate section = map sections@@ -222,16 +230,16 @@ , "<a class=\"pilcrow\" href=\"#section-" , show x' , "\">λ</a></div>"- , L.unpack $ sect M.! "docsHtml"+ , T.unpack $ sect M.! "docsHtml" , "</td><td class=\"code\">"- , L.unpack $ sect M.! "codeHtml"+ , T.unpack $ sect M.! "codeHtml" , "</td></tr>" ]) -- Once all of the code is finished highlighting, we can generate the HTML -- file and write out the documentation. Pass the completed sections into -- the template found in `resources/hyakko.html`-generateHTML :: FilePath -> [Map String ByteString] -> IO ()+generateHTML :: FilePath -> [Map String Text] -> IO () generateHTML src section = do let title = takeFileName src dest = destination src@@ -243,7 +251,7 @@ , sectionTemplate section [0 .. (length section) - 1] ] putStrLn $ "hyakko: " ++ src ++ " -> " ++ dest- L.writeFile dest html+ T.writeFile dest html -- ### Helpers & Setup @@ -266,10 +274,10 @@ ("name", "ruby"), hashSymbol]) ] -- Does the line begin with a comment?- hasComments symbol = "^\\s*" >< symbol >< "\\s?"+ hasComments symbol = "^\\s*" ++* symbol ++* "\\s?" -- The dividing token we feed into Pygments, to delimit the boundaries -- between sections.- tokenDivider symbol = "\n" >< symbol >< "DIVIDER\n"+ tokenDivider symbol = "\n" ++* symbol ++* "DIVIDER\n" -- The mirror of `divider_text` that we expect Pygments to return. We -- can split on this to recover the original sections. **Note**: the -- class is "c" for Python and "c1" for the other languages@@ -295,25 +303,25 @@ destination fp = "docs" </> (takeBaseName fp) ++ ".html" -- Create the template that we will use to generate the Hyakko HTML page.-hyakkoTemplate :: [(String, String)] -> IO ByteString+hyakkoTemplate :: [(String, String)] -> IO Text hyakkoTemplate var = readDataFile "resources/hyakko.html" >>=- return . renderTemplate var . L.unpack+ return . T.pack . renderTemplate var . T.unpack -- The CSS styles we'd like to apply to the documentation.-hyakkoStyles :: IO ByteString+hyakkoStyles :: IO Text hyakkoStyles = readDataFile "resources/hyakko.css" -- The start and end of each Pygments highlight block.-highlightStart, highlightEnd :: ByteString+highlightStart, highlightEnd :: Text highlightStart = "<div class=\"highlight\"><pre>" highlightEnd = "</pre></div>" highlightReplace :: String-highlightReplace = L.unpack highlightStart ++ "|" ++ L.unpack highlightEnd+highlightReplace = T.unpack highlightStart ++ "|" ++ T.unpack highlightEnd -- Reads from resource path given in cabal package-readDataFile :: FilePath -> IO ByteString-readDataFile = getDataFileName >=> L.readFile+readDataFile :: FilePath -> IO Text+readDataFile = getDataFileName >=> T.readFile -- For each source file passed in as an argument, generate the -- documentation.@@ -346,5 +354,5 @@ style <- hyakkoStyles source <- sources createDirectoryIfMissing False "docs"- L.writeFile "docs/hyakko.css" style+ T.writeFile "docs/hyakko.css" style generateDocumentation source
src/Text/Markdown.hs view
@@ -5,11 +5,11 @@ , def ) -import Data.ByteString.Lazy.Char8 (ByteString, pack)+import Data.Text (Text, pack) -- Function for translating Markdown to HTML since `Pandoc` has several -- different generators for other markup languages.-toHTML :: String -> ByteString+toHTML :: String -> Text toHTML = pack . writeHTMLStr . parse where parse = readMarkdown def writeHTMLStr = writeHtmlString def