hyakko 0.5.1 → 0.5.2
raw patch · 2 files changed
+81/−45 lines, 2 filesdep +cmdargs
Dependencies added: cmdargs
Files
- hyakko.cabal +3/−2
- src/Hyakko.lhs +78/−43
hyakko.cabal view
@@ -1,5 +1,5 @@ name: hyakko-version: 0.5.1+version: 0.5.2 cabal-version: >= 1.6 build-type: Simple license: MIT@@ -41,7 +41,8 @@ bytestring >= 0.9, text >= 0.11, highlighting-kate >= 0.5,- blaze-html >= 0.5+ blaze-html >= 0.5,+ cmdargs >= 0.10 hs-source-dirs: src ghc-options: -O2 -Wall main-is: Hyakko.lhs
src/Hyakko.lhs view
@@ -29,7 +29,7 @@ cabal update cabal install hyakko -> {-# LANGUAGE OverloadedStrings #-}+> {-# LANGUAGE OverloadedStrings, DeriveDataTypeable #-} > module Main where @@ -43,25 +43,26 @@ > import qualified Data.Text as T > import qualified Data.Text.IO as T > import Data.List (sort)-> import Data.Maybe (fromJust)+> import Data.Maybe (fromJust, isNothing)+> import Data.Version (showVersion) > import Control.Monad (filterM, (>=>), forM) > import qualified Text.Blaze.Html as B > import Text.Blaze.Html.Renderer.Utf8 (renderHtml) > import qualified Text.Highlighting.Kate as K > import Text.Pandoc.Templates > import Text.Regex.PCRE ((=~))+> import System.Console.CmdArgs > import System.Directory ( getDirectoryContents > , doesDirectoryExist > , doesFileExist > , createDirectoryIfMissing > )-> import System.Environment (getArgs) > import System.FilePath ( takeBaseName > , takeExtension > , takeFileName > , (</>) > )-> import Paths_hyakko (getDataFileName)+> import Paths_hyakko (getDataFileName, version) Main Documentation Generation Functions ---------------------------------------@@ -88,19 +89,21 @@ 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 <- T.readFile x-> let sections = parse (getLanguage x) code-> if null sections then-> putStrLn $ "hyakko doesn't support the language extension "-> ++ takeExtension x-> else do-> let output = highlight x sections-> y = mapSections sections output-> generateHTML x y-> generateDocumentation xs+> generateDocumentation :: Hyakko -> [FilePath] -> IO ()+> generateDocumentation _ [] =+> putStrLn "hyakko: no files or options given (try --help)"+> generateDocumentation opts xs = mapM_ generate xs+> where generate :: FilePath -> IO ()+> generate x = do+> code <- T.readFile x+> let sections = parse (getLanguage x) code+> if null sections then+> putStrLn $ "hyakko doesn't support the language extension "+> ++ takeExtension x+> else do+> let highlighted = highlight x sections+> y = mapSections sections highlighted+> generateHTML opts x y Given a string of source code, parse out each comment and the code that follows it, and create an individual **section** for it. Sections take the@@ -222,11 +225,11 @@ <a class="source" href="$href-link$">$file-name$</a> -> sourceTemplate :: [FilePath] -> [(String, String)]-> sourceTemplate = map source+> sourceTemplate :: Hyakko -> [FilePath] -> [(String, String)]+> sourceTemplate opts = map source > where source x = ("source", concat > [ "<a class=\"source\" href=\""-> , takeFileName $ destination x+> , takeFileName $ destination (output opts) x > , "\">" > , takeFileName x > , "</a>"@@ -271,15 +274,15 @@ and write out the documentation. Pass the completed sections into the template found in `resources/hyakko.html` -> generateHTML :: FilePath -> [Map String Text] -> IO ()-> generateHTML src section = do-> let title = takeFileName src-> dest = destination src-> source <- sources-> html <- hyakkoTemplate $ concat+> generateHTML :: Hyakko -> FilePath -> [Map String Text] -> IO ()+> generateHTML opts src section = do+> let title = takeFileName src+> dest = destination (output opts) src+> source <- sources $ dirOrFiles opts+> html <- hyakkoTemplate (template opts) $ concat > [ [("title", title)] > , multiTemplate $ length source-> , sourceTemplate source+> , sourceTemplate opts source > , sectionTemplate section [0 .. (length section) - 1] > ] > putStrLn $ "hyakko: " ++ src ++ " -> " ++ dest@@ -330,19 +333,24 @@ Compute the destination HTML path for an input source file path. If the source is `lib/example.hs`, the HTML will be at docs/example.html -> destination :: FilePath -> FilePath-> destination fp = "docs" </> (takeBaseName fp) ++ ".html"+> destination :: FilePath -> FilePath -> FilePath+> destination out fp = out </> (takeBaseName fp) ++ ".html" Create the template that we will use to generate the Hyakko HTML page. -> hyakkoTemplate :: [(String, String)] -> IO Text-> hyakkoTemplate var = readDataFile "resources/hyakko.html" >>=-> return . T.pack . renderTemplate var . T.unpack+> hyakkoTemplate :: Maybe FilePath -> [(String, String)] -> IO Text+> hyakkoTemplate maybeFile var = do+> content <- if isNothing maybeFile then+> readDataFile "resources/hyakko.html"+> else+> T.readFile $ fromJust maybeFile+> return . T.pack . renderTemplate var $ T.unpack content The CSS styles we'd like to apply to the documentation. -> hyakkoStyles :: IO Text-> hyakkoStyles = readDataFile "resources/hyakko.css"+> hyakkoStyles :: Maybe FilePath -> IO Text+> hyakkoStyles Nothing = readDataFile "resources/hyakko.css"+> hyakkoStyles (Just file) = T.readFile file Reads from resource path given in cabal package @@ -351,10 +359,9 @@ For each source file passed in as an argument, generate the documentation. -> sources :: IO [FilePath]-> sources = do-> args <- getArgs-> files <- forM args $ \x -> do+> sources :: [FilePath] -> IO [FilePath]+> sources file = do+> files <- forM file $ \x -> do > isDir <- doesDirectoryExist x > if isDir then > unpackDirectories x@@ -375,12 +382,40 @@ > subcontent <- mapM unpackDirectories subdir >>= return . concat > return (files ++ subcontent) +Configuration+-------------++Data structure for command line argument parsing.++> data Hyakko =+> Hyakko { output :: FilePath+> , css :: Maybe FilePath+> , template :: Maybe FilePath+> , dirOrFiles :: [FilePath]+> } deriving (Show, Data, Typeable)++Default configuration **options**. If no arguments for these flags are+specifed, it will just use the ones in `defaultConfig`.++> defaultConfig :: Hyakko+> defaultConfig = Hyakko+> { output = "docs" &= typDir+> &= help "use a custom output path"+> , css = Nothing &= typFile+> &= help "use a custom css file"+> , template = Nothing &= typFile+> &= help "use a custom pandoc template"+> , dirOrFiles = [] &= args &= typ "FILES/DIRS"+> } &= summary ("hyakko v" ++ showVersion version)+ Run the script. > main :: IO () > main = do-> style <- hyakkoStyles-> source <- sources-> createDirectoryIfMissing False "docs"-> T.writeFile "docs/hyakko.css" style-> generateDocumentation source+> opts <- cmdArgs defaultConfig+> style <- hyakkoStyles $ css opts+> source <- sources $ dirOrFiles opts+> let dirout = output opts+> createDirectoryIfMissing False dirout+> T.writeFile (dirout </> "hyakko.css") style+> generateDocumentation opts source