module Text.Docvim.Printer.Markdown (markdown) where
import Control.Monad.Reader
import Data.List
import Data.Maybe
import Text.Docvim.AST
import Text.Docvim.Parse
import Text.Docvim.Visitor.Plugin
import Text.Docvim.Visitor.Symbol
data Metadata = Metadata { pluginName :: Maybe String
, symbols :: [String]
}
type Env = Reader Metadata String
data Anchor = Anchor [Attribute] String
data Attribute = Attribute String String
markdown :: Node -> String
markdown n = if null stripped
then ""
else stripped ++ "\n"
where
metadata = Metadata (getPluginName n) (getSymbols n)
stripped = rstrip (runReader (node n) metadata)
nodes :: [Node] -> Env
nodes ns = concat <$> mapM node ns
node :: Node -> Env
node n = case n of
Blockquote b -> blockquote b >>= nl >>= nl
BreakTag -> return "<br />"
Code c -> return $ "`" ++ c ++ "`"
CommandAnnotation {} -> command n
CommandsAnnotation -> h2 "Commands"
DocBlock d -> nodes d
Fenced f -> return $ fenced f ++ "\n\n"
FunctionDeclaration {} -> nodes $ functionBody n
FunctionsAnnotation -> h2 "Functions"
HeadingAnnotation h -> h2 h
Link l -> link l
LinkTargets l -> return $ linkTargets l
List ls -> nodes ls >>= nl
ListItem l -> fmap ("- " ++) (nodes l) >>= nl
MappingAnnotation m -> mapping m
MappingsAnnotation -> h2 "Mappings"
OptionAnnotation {} -> option n
OptionsAnnotation -> h2 "Options"
Paragraph p -> nodes p >>= nl >>= nl
Plaintext p -> return p
PluginAnnotation name _ -> h1 name
Project p -> nodes p
Separator -> return $ "---" ++ "\n\n"
SubheadingAnnotation s -> h3 s
Unit u -> nodes u
Whitespace -> return " "
_ -> return ""
nl :: String -> Env
nl = return . (++ "\n")
blockquote :: [Node] -> Env
blockquote ps = do
ps' <- mapM paragraph ps
return $ "> " ++ intercalate "\n>\n> " ps'
where
paragraph p = fmap trim (node p)
trim contents = take (length contents 2) contents
link :: String -> Env
link l = do
metadata <- ask
return $ if l `elem` symbols metadata
then "<strong>[`" ++ l ++ "`](" ++ gitHubAnchor l ++ ")</strong>"
else "<strong>`" ++ l ++ "`</strong>"
fenced :: [String] -> String
fenced f = "```\n" ++ code ++ "```"
where code = if null f
then ""
else intercalate "\n" f ++ "\n"
linkTargets :: [String] -> String
linkTargets ls = "<p align=\"right\">"
++ unwords (map linkify $ sort ls)
++ "</p>"
++ "\n"
where
linkify l = a $ Anchor [ Attribute "name" (sanitizeAnchor l)
, Attribute "href" (gitHubAnchor l)
]
(codify l)
h1 :: String -> Env
h1 = heading 1
h2 :: String -> Env
h2 = heading 2
h3 :: String -> Env
h3 = heading 3
heading :: Int -> String -> Env
heading level string = do
metadata <- ask
return $ replicate level '#' ++ " " ++ string ++ anch (pluginName metadata) ++ "\n\n"
where
anch name = a $ Anchor [ Attribute "name" (sanitizeAnchor $ pre ++ string)
, Attribute "href" (gitHubAnchor $ pre ++ string)
]
""
where
pre = maybe "" (++ "-") name
codify :: String -> String
codify s = "<code>" ++ s ++ "</code>"
a :: Anchor -> String
a (Anchor attributes target) = "<a" ++ attrs ++ ">" ++ target ++ "</a>"
where
attrs = if not (null attributes)
then " " ++ attributesString attributes
else ""
attributesString :: [Attribute] -> String
attributesString as = unwords (map attributeToString as)
where attributeToString (Attribute name value) = name ++ "=\"" ++ value ++ "\""
gitHubAnchor :: String -> String
gitHubAnchor n = "#user-content-" ++ sanitizeAnchor n
option :: Node -> Env
option (OptionAnnotation n t d) = do
h <- h3 $ "`" ++ n ++ "` (" ++ t ++ ", default: " ++ def ++ ")"
return $ targets ++ h
where targets = linkTargets [n]
def = fromMaybe "none" d
option _ = invalidNode
command :: Node -> Env
command (CommandAnnotation name params) = do
content <- h3 $ "`:" ++ annotation ++ "`"
return $ target ++ content
where target = linkTargets [":" ++ name]
annotation = rstrip $ name ++ " " ++ fromMaybe "" params
command _ = invalidNode
mapping :: String -> Env
mapping name = h3 $ "`" ++ name ++ "`"