{- | Module : $Header$ Description : Generating HTML documentation Copyright : (c) 2011 - 2015, Björn Peemöller License : OtherLicense Maintainer : bjp@informatik.uni-kiel.de Stability : experimental Portability : portable This module defines a function for generating HTML documentation pages for Curry source modules. -} module Html.CurryHtml (source2html) where import Control.Monad.Writer import Data.List (mapAccumL) import Data.Maybe (fromMaybe, isJust) import Network.URI (escapeURIString, isUnreserved) import System.Directory (copyFile, doesFileExist) import System.FilePath ((>)) import Curry.Base.Ident ( ModuleIdent (..), QualIdent (..), unqualify , moduleName) import Curry.Base.Monad (CYIO, liftCYM, failMessages) import Curry.Base.Pretty ((<+>), text, vcat) import Curry.Files.PathUtils (readModule) import Curry.Syntax (Module (..), lexSource) import Html.SyntaxColoring import Base.Messages (message) import CompilerOpts (Options (..), WarnOpts (..)) import CurryBuilder (buildCurry, findCurry) import Modules (loadAndCheckModule) import Transformations (qual) import Paths_curry_frontend (getDataFileName) -- |'FilePath' of the CSS style file to be added to the documentation. cssFile :: FilePath cssFile = "currysource.css" -- |Translate source file into HTML file with syntaxcoloring source2html :: Options -> String -> CYIO () source2html opts s = do srcFile <- findCurry opts s (mid, doc) <- docModule opts srcFile let outDir = fromMaybe "." $ optHtmlDir opts outFile = outDir > htmlFile mid liftIO $ writeFile outFile doc updateCSSFile outDir -- |Update the CSS file updateCSSFile :: FilePath -> CYIO () updateCSSFile dir = do src <- liftIO $ getDataFileName cssFile let target = dir > cssFile srcExists <- liftIO $ doesFileExist src if srcExists then liftIO $ copyFile src target else failMessages [message $ missingStyleFile src] where missingStyleFile f = vcat [ text "Could not copy CSS style file:" , text "File" <+> text ("`" ++ f ++ "'") <+> text "does not exist" ] -- |Create the documentation for the module docModule :: Options -> String -> CYIO (ModuleIdent, String) docModule opts f = do mbModule <- liftIO $ readModule f case mbModule of Nothing -> failMessages [message $ text $ "Missing file: " ++ f] Just src -> do toks <- liftCYM $ lexSource f src typed@(Module _ m _ _ _) <- fullParse opts f src return (m, program2html m $ genProgram f typed toks) -- |Return the syntax tree of the source program 'src' (type 'Module'; see -- Module "CurrySyntax").after inferring the types of identifiers. -- 'fullParse' always searches for standard Curry libraries in the path -- defined in the -- environment variable "PAKCSLIBPATH". Additional search paths can -- be defined using the argument 'paths'. fullParse :: Options -> FilePath -> String -> CYIO Module fullParse opts fn _ = do buildCurry (opts { optTargetTypes = []}) fn (env, mdl) <- loadAndCheckModule opts' fn return (snd $ qual (env, mdl)) where opts' = opts { optWarnOpts = (optWarnOpts opts) { wnWarn = False } , optTargetTypes = [] } -- generates htmlcode with syntax highlighting -- @param modulname -- @param a program -- @return HTMLcode program2html :: ModuleIdent -> [Code] -> String program2html m codes = unlines [ "" , "", "
" , "" , "" ++ lineHtml ++ " | "
, "" ++ codeHtml ++ " | "
, "