module Data.GI.CodeGen.Haddock
( deprecatedPragma
, writeDocumentation
, RelativeDocPosition(..)
, writeHaddock
, writeArgDocumentation
, writeReturnDocumentation
, addSectionDocumentation
) where
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative ((<$>))
#endif
import Control.Monad (mapM_, unless)
import qualified Data.Map as M
import Data.Monoid ((<>))
import qualified Data.Text as T
import Data.Text (Text)
import Data.GI.GIR.Arg (Arg(..))
import Data.GI.GIR.Callable (Callable(..))
import Data.GI.GIR.Deprecation (DeprecationInfo(..))
import Data.GI.GIR.Documentation (Documentation(..))
import Data.GI.CodeGen.Code (CodeGen, config, line, HaddockSection,
getC2HMap, addSectionFormattedDocs)
import Data.GI.CodeGen.Config (modName, overrides)
import Data.GI.CodeGen.CtoHaskellMap (Hyperlink(..))
import Data.GI.CodeGen.GtkDoc (GtkDoc(..), Token(..), CRef(..), Language(..),
Link(..), ListItem(..), parseGtkDoc)
import Data.GI.CodeGen.Overrides (onlineDocsMap)
import Data.GI.CodeGen.SymbolNaming (lowerSymbol)
data RelativeDocPosition = DocBeforeSymbol
| DocAfterSymbol
formatHaddock :: M.Map CRef Hyperlink -> Text -> GtkDoc -> Text
formatHaddock c2h docBase (GtkDoc doc) = T.concat $ map formatToken doc
where formatToken :: Token -> Text
formatToken (Literal l) = escape l
formatToken (Verbatim v) = "@" <> escape v <> "@"
formatToken (CodeBlock l c) = formatCodeBlock l c
formatToken (ExternalLink l) = formatLink l docBase
formatToken (Image l) = formatImage l docBase
formatToken (SectionHeader l h) = formatSectionHeader c2h docBase l h
formatToken (List l) = formatList c2h docBase l
formatToken (SymbolRef (ParamRef p)) = "/@" <> lowerSymbol p <> "@/"
formatToken (SymbolRef cr) = case M.lookup cr c2h of
Just hr -> formatHyperlink hr
Nothing -> formatUnknownCRef c2h cr
formatUnknownCRef :: M.Map CRef Hyperlink -> CRef -> Text
formatUnknownCRef _ (FunctionRef f) = formatCRef $ f <> "()"
formatUnknownCRef _ (ParamRef _) = error $ "Should not be reached"
formatUnknownCRef c2h (SignalRef owner signal) =
case M.lookup (TypeRef owner) c2h of
Nothing -> formatCRef $ owner <> "::" <> signal
Just r -> formatHyperlink r <> "::" <> formatCRef signal
formatUnknownCRef c2h (PropertyRef owner prop) =
case M.lookup (TypeRef owner) c2h of
Nothing -> formatCRef $ owner <> ":" <> prop
Just r -> formatHyperlink r <> ":" <> formatCRef prop
formatUnknownCRef c2h (VMethodRef owner vmethod) =
case M.lookup (TypeRef owner) c2h of
Nothing -> formatCRef $ owner <> "." <> vmethod <> "()"
Just r -> formatHyperlink r <> "." <> formatCRef vmethod <> "()"
formatUnknownCRef c2h (StructFieldRef owner field) =
case M.lookup (TypeRef owner) c2h of
Nothing -> formatCRef $ owner <> "." <> field
Just r -> formatHyperlink r <> "." <> formatCRef field
formatUnknownCRef _ (TypeRef t) = formatCRef t
formatUnknownCRef _ (ConstantRef t) = formatCRef t
formatCRef :: Text -> Text
formatCRef t = "@/" <> escape t <> "/@"
formatHyperlink :: Hyperlink -> Text
formatHyperlink (IdentifierLink t) = "'" <> t <> "'"
formatHyperlink (ModuleLink m) = "\"" <> m <> "\""
formatHyperlink (ModuleLinkWithAnchor m a) = "\"" <> m <> "#" <> a <> "\""
formatCodeBlock :: Maybe Language -> Text -> Text
formatCodeBlock maybeLang code =
let header = case maybeLang of
Nothing -> ""
Just (Language lang) -> "\n=== /" <> lang <> " code/\n"
birdTrack = T.unlines . map (T.cons '>') . T.lines
in header <> birdTrack code
qualifiedWith :: Text -> Text -> Text
qualifiedWith address docBase =
if "http://" `T.isPrefixOf` address || "https://" `T.isPrefixOf` address
then address
else if "/" `T.isSuffixOf` docBase
then docBase <> address
else docBase <> "/" <> address
formatLink :: Link -> Text -> Text
formatLink (Link {linkName = name, linkAddress = address}) docBase =
let address' = address `qualifiedWith` docBase
name' = T.replace ">" "\\>" name
in "<" <> address' <> " " <> name' <> ">"
formatImage :: Link -> Text -> Text
formatImage (Link {linkName = name, linkAddress = address}) docBase =
let address' = address `qualifiedWith` docBase
name' = T.replace ">" "\\>" name
in if T.null name'
then "<<" <> address' <> ">>"
else "<<" <> address' <> " " <> name' <> ">>"
formatSectionHeader :: M.Map CRef Hyperlink -> Text -> Int -> GtkDoc -> Text
formatSectionHeader c2h docBase level header =
T.replicate level "=" <> " " <> formatHaddock c2h docBase header <> "\n"
formatList :: M.Map CRef Hyperlink -> Text -> [ListItem] -> Text
formatList c2h docBase items = "\n" <> T.concat (map formatListItem items)
where formatListItem :: ListItem -> Text
formatListItem (ListItem first rest) =
"* " <> format first <> "\n"
<> T.concat (map ((<> "\n") . format) rest)
format :: GtkDoc -> Text
format = formatHaddock c2h docBase
escape :: Text -> Text
escape = T.concatMap escapeChar
where
escapeChar :: Char -> Text
escapeChar c = if c `elem` ("\\/'`\"@<" :: [Char])
then "\\" <> T.singleton c
else T.singleton c
getDocBase :: CodeGen Text
getDocBase = do
mod <- modName <$> config
docsMap <- (onlineDocsMap . overrides) <$> config
return $ case M.lookup mod docsMap of
Just url -> url
Nothing -> "http://developer.gnome.org/" <> T.toLower mod <>
"/stable"
deprecatedPragma :: Text -> Maybe DeprecationInfo -> CodeGen ()
deprecatedPragma _ Nothing = return ()
deprecatedPragma name (Just info) = do
c2h <- getC2HMap
docBase <- getDocBase
line $ "{-# DEPRECATED " <> name <> " " <>
(T.pack . show) (note <> reason c2h docBase) <> " #-}"
where reason c2h docBase =
case deprecationMessage info of
Nothing -> []
Just msg -> map (formatHaddock c2h docBase . parseGtkDoc)
(T.lines msg)
note = case deprecatedSinceVersion info of
Nothing -> []
Just v -> ["(Since version " <> v <> ")"]
formatDocumentation :: M.Map CRef Hyperlink -> Text -> Documentation -> Text
formatDocumentation c2h docBase doc = do
let description = case rawDocText doc of
Just raw -> formatHaddock c2h docBase (parseGtkDoc raw)
Nothing -> "/No description available in the introspection data./"
description <> case sinceVersion doc of
Nothing -> ""
Just ver -> "\n\n/Since: " <> ver <> "/"
writeDocumentation :: RelativeDocPosition -> Documentation -> CodeGen ()
writeDocumentation pos doc = do
line $ case pos of
DocBeforeSymbol -> "{- |"
DocAfterSymbol -> "{- ^"
c2h <- getC2HMap
docBase <- getDocBase
let haddock = formatDocumentation c2h docBase doc
mapM_ line (T.lines haddock)
line "-}"
writeHaddock :: RelativeDocPosition -> Text -> CodeGen ()
writeHaddock pos haddock =
let marker = case pos of
DocBeforeSymbol -> "|"
DocAfterSymbol -> "^"
in if T.any (== '\n') haddock
then do
line $ "{- " <> marker
mapM_ line (T.lines haddock)
line $ "-}"
else line $ "-- " <> marker <> " " <> haddock
writeArgDocumentation :: Arg -> CodeGen ()
writeArgDocumentation arg =
case rawDocText (argDoc arg) of
Nothing -> return ()
Just raw -> do
c2h <- getC2HMap
docBase <- getDocBase
line $ "{- ^ /@" <> lowerSymbol (argCName arg) <> "@/: " <>
formatHaddock c2h docBase (parseGtkDoc raw) <> " -}"
writeReturnDocumentation :: Callable -> Bool -> CodeGen ()
writeReturnDocumentation callable skip = do
c2h <- getC2HMap
docBase <- getDocBase
let returnValInfo = if skip
then []
else case rawDocText (returnDocumentation callable) of
Nothing -> []
Just raw -> ["__Returns:__ " <>
formatHaddock c2h docBase
(parseGtkDoc raw)]
throwsInfo = if callableThrows callable
then ["/(Can throw 'Data.GI.Base.GError.GError')/"]
else []
let fullInfo = T.intercalate " " (returnValInfo ++ throwsInfo)
unless (T.null fullInfo) $
line $ "{- ^ " <> fullInfo <> " -}"
addSectionDocumentation :: HaddockSection -> Documentation -> CodeGen ()
addSectionDocumentation section doc = do
c2h <- getC2HMap
docBase <- getDocBase
let formatted = formatDocumentation c2h docBase doc
addSectionFormattedDocs section formatted