Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new flags to the Html backend #2447

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions app/Commands/Html.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ runGenOnlySourceHtml HtmlOptions {..} = do
Html.genSourceHtml
GenSourceHtmlArgs
{ _genSourceHtmlArgsAssetsDir = _htmlAssetsPrefix,
_genSourceHtmlArgsHtmlKind = Html.HtmlSrc,
_genSourceHtmlArgsHtmlKind = Html.HtmlOnly,
_genSourceHtmlArgsOnlyCode = _htmlOnlyCode,
_genSourceHtmlArgsParamBase = "",
_genSourceHtmlArgsUrlPrefix = _htmlUrlPrefix,
_genSourceHtmlArgsIdPrefix = _htmlIdPrefix,
_genSourceHtmlArgsNoPath = _htmlNoPath,
_genSourceHtmlArgsConcreteOpts = Concrete.defaultOptions,
_genSourceHtmlArgsModule = m,
_genSourceHtmlArgsComments = res ^. comments,
Expand All @@ -47,9 +50,11 @@ runCommand HtmlOptions {..}
_judocArgsCtx = ctx,
_judocArgsOutputDir = outputDir,
_judocArgsUrlPrefix = _htmlUrlPrefix,
_judocArgsIdPrefix = _htmlIdPrefix,
_judocArgsTheme = _htmlTheme,
_judocArgsNonRecursive = _htmlNonRecursive,
_judocArgsNoFooter = _htmlNoFooter
_judocArgsNoFooter = _htmlNoFooter,
_judocArgsNoPath = _htmlNoPath
}
when _htmlOpen $ case openCmd of
Nothing -> say "Could not recognize the 'open' command for your OS"
Expand Down
22 changes: 21 additions & 1 deletion app/Commands/Html/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import Juvix.Compiler.Backend.Html.Data.Options hiding (HtmlOptions)
data HtmlOptions = HtmlOptions
{ _htmlNonRecursive :: Bool,
_htmlOnlySource :: Bool,
_htmlOnlyCode :: Bool,
_htmlTheme :: Theme,
_htmlOutputDir :: AppPath Dir,
_htmlInputFile :: AppPath File,
_htmlNoFooter :: Bool,
_htmlNoPath :: Bool,
_htmlAssetsPrefix :: Text,
_htmlUrlPrefix :: Text,
_htmlIdPrefix :: Text,
_htmlOpen :: Bool
}
deriving stock (Data)
Expand All @@ -23,13 +26,18 @@ parseHtml = do
_htmlNonRecursive <-
switch
( long "non-recursive"
<> help "Don't process imported modules recursively"
<> help "Do not process imported modules recursively"
)
_htmlOnlySource <-
switch
( long "only-source"
<> help "Generate only Html for the source code with syntax highlighting"
)
_htmlOnlyCode <-
switch
( long "only-code"
<> help "If --only-source is enabled, only generate the code without the header and footer"
)
_htmlTheme <-
option
(eitherReader parseTheme)
Expand All @@ -52,6 +60,11 @@ parseHtml = do
( long "no-footer"
<> help "Remove HTML Juvix footer"
)
_htmlNoPath <-
switch
( long "no-path"
<> help "Remove the path from all hyperlinks"
)
_htmlAssetsPrefix <-
strOption
( value ""
Expand All @@ -66,6 +79,13 @@ parseHtml = do
<> showDefault
<> help "Prefix used for inner Juvix hyperlinks"
)
_htmlIdPrefix <-
strOption
( value ""
<> long "prefix-id"
<> showDefault
<> help "Prefix used for HTML element IDs"
)
_htmlOpen <-
switch
( long "open"
Expand Down
3 changes: 3 additions & 0 deletions src/Juvix/Compiler/Backend/Html/Data/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ data HtmlOptions = HtmlOptions
{ _htmlOptionsKind :: HtmlKind,
_htmlOptionsAssetsPrefix :: Text,
_htmlOptionsUrlPrefix :: Text,
_htmlOptionsIdPrefix :: Text,
_htmlOptionsOnlyCode :: Bool,
_htmlOptionsNoPath :: Bool,
_htmlOptionsOutputDir :: Path Abs Dir,
_htmlOptionsParamBase :: Text,
_htmlOptionsTheme :: Theme,
Expand Down
9 changes: 7 additions & 2 deletions src/Juvix/Compiler/Backend/Html/Translation/FromTyped.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ data JudocArgs = JudocArgs
_judocArgsCtx :: InternalTypedResult,
_judocArgsTheme :: Theme,
_judocArgsNonRecursive :: Bool,
_judocArgsNoFooter :: Bool
_judocArgsNoFooter :: Bool,
_judocArgsIdPrefix :: Text,
_judocArgsNoPath :: Bool
}

makeLenses ''JudocArgs
Expand Down Expand Up @@ -190,9 +192,12 @@ genJudocHtml JudocArgs {..} =
_htmlOptionsAssetsPrefix = _judocArgsAssetsPrefix,
_htmlOptionsOutputDir = _judocArgsOutputDir,
_htmlOptionsUrlPrefix = _judocArgsUrlPrefix,
_htmlOptionsIdPrefix = _judocArgsIdPrefix,
_htmlOptionsParamBase = _judocArgsBaseName,
_htmlOptionsTheme = _judocArgsTheme,
_htmlOptionsNoFooter = _judocArgsNoFooter
_htmlOptionsNoFooter = _judocArgsNoFooter,
_htmlOptionsOnlyCode = False,
_htmlOptionsNoPath = _judocArgsNoPath
}

allModules
Expand Down
64 changes: 41 additions & 23 deletions src/Juvix/Compiler/Backend/Html/Translation/FromTyped/Source.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ data GenSourceHtmlArgs = GenSourceHtmlArgs
_genSourceHtmlArgsHtmlKind :: HtmlKind,
_genSourceHtmlArgsParamBase :: Text,
_genSourceHtmlArgsUrlPrefix :: Text,
_genSourceHtmlArgsIdPrefix :: Text,
_genSourceHtmlArgsOnlyCode :: Bool,
_genSourceHtmlArgsModule :: Module 'Scoped 'ModuleTop,
_genSourceHtmlArgsOutputDir :: Path Abs Dir,
_genSourceHtmlArgsNonRecursive :: Bool,
_genSourceHtmlArgsNoFooter :: Bool,
_genSourceHtmlArgsNoPath :: Bool,
_genSourceHtmlArgsComments :: Comments,
_genSourceHtmlArgsTheme :: Theme
}
Expand Down Expand Up @@ -89,11 +92,14 @@ genSourceHtml o@GenSourceHtmlArgs {..} = do
HtmlOptions
{ _htmlOptionsOutputDir = o ^. genSourceHtmlArgsOutputDir,
_htmlOptionsUrlPrefix = o ^. genSourceHtmlArgsUrlPrefix,
_htmlOptionsIdPrefix = o ^. genSourceHtmlArgsIdPrefix,
_htmlOptionsOnlyCode = o ^. genSourceHtmlArgsOnlyCode,
_htmlOptionsAssetsPrefix = o ^. genSourceHtmlArgsAssetsDir,
_htmlOptionsKind = o ^. genSourceHtmlArgsHtmlKind,
_htmlOptionsParamBase = o ^. genSourceHtmlArgsParamBase,
_htmlOptionsTheme = o ^. genSourceHtmlArgsTheme,
_htmlOptionsNoFooter = o ^. genSourceHtmlArgsNoFooter
_htmlOptionsNoFooter = o ^. genSourceHtmlArgsNoFooter,
_htmlOptionsNoPath = o ^. genSourceHtmlArgsNoPath
}

entry = o ^. genSourceHtmlArgsModule
Expand Down Expand Up @@ -148,10 +154,15 @@ genModuleHtml ::
GenModuleHtmlArgs ->
Sem r Html
genModuleHtml o = do
outHtml <- mhead <> mbody
return $
docTypeHtml ! Attr.xmlns "http://www.w3.org/1999/xhtml" $
outHtml
onlyCode <- asks (^. htmlOptionsOnlyCode)
if
| onlyCode -> justCode
| otherwise -> do
hd <- mhead
bd <- mbody
return $
docTypeHtml ! Attr.xmlns "http://www.w3.org/1999/xhtml" $
hd <> bd
where
mhead :: Sem r Html
mhead = do
Expand All @@ -166,7 +177,7 @@ genModuleHtml o = do
mbody =
fold
[ mheader,
prettySrc,
preTagCode,
htmlJuvixFooter,
formattedTime
]
Expand All @@ -181,14 +192,15 @@ genModuleHtml o = do
"%Y-%m-%d %-H:%M %Z"
(o ^. genModuleHtmlArgsUTC)

prettySrc :: Sem r Html
prettySrc = do
pp <-
ppModuleSrcHtml
(o ^. genModuleHtmlArgsConcreteOpts)
(o ^. genModuleHtmlArgsComments)
(o ^. genModuleHtmlArgsModule)
return $ (pre ! Attr.id "src-content") pp
justCode :: Sem r Html
justCode =
ppModuleSrcHtml
(o ^. genModuleHtmlArgsConcreteOpts)
(o ^. genModuleHtmlArgsComments)
(o ^. genModuleHtmlArgsModule)

preTagCode :: Sem r Html
preTagCode = (pre ! Attr.id "src-content") <$> justCode

mheader :: Sem r Html
mheader =
Expand Down Expand Up @@ -305,7 +317,8 @@ putTag ann x = case ann of
tagDef :: TopModulePath -> S.NameId -> Sem r Html
tagDef tmp nid = do
ref' <- tagRef tmp nid
return $ (Html.span ! Attr.id (nameIdAttr nid)) ref'
attrId <- nameIdAttr nid
return $ (Html.span ! Attr.id attrId) ref'

tagRef :: TopModulePath -> S.NameId -> Sem r Html
tagRef tmp ni = do
Expand All @@ -330,8 +343,10 @@ putTag ann x = case ann of
S.KNameFixity -> JuFixity
)

nameIdAttr :: S.NameId -> AttributeValue
nameIdAttr (S.NameId k) = fromString . show $ k
nameIdAttr :: (Members '[Reader HtmlOptions] r) => S.NameId -> Sem r AttributeValue
nameIdAttr (S.NameId k) = do
pfx <- unpack <$> asks (^. htmlOptionsIdPrefix)
return $ fromString $ pfx <> show k

moduleDocRelativePath :: (Members '[Reader HtmlOptions] r) => TopModulePath -> Sem r (Path Rel File)
moduleDocRelativePath m = do
Expand All @@ -340,10 +355,13 @@ moduleDocRelativePath m = do

nameIdAttrRef :: (Members '[Reader HtmlOptions] r) => TopModulePath -> Maybe S.NameId -> Sem r AttributeValue
nameIdAttrRef tp s = do
pth <- toFilePath <$> moduleDocRelativePath tp
prefixUrl <- unpack <$> asks (^. htmlOptionsUrlPrefix)
return $
fromString prefixUrl
<> fromString pth
<> preEscapedToValue '#'
<>? (nameIdAttr <$> s)
path <- toFilePath <$> moduleDocRelativePath tp
noPath <- asks (^. htmlOptionsNoPath)
let prefix = prefixUrl <> if noPath then "" else path
attr <-
maybe
(return mempty)
(((preEscapedToValue '#' <>) <$>) . nameIdAttr)
s
return $ fromString prefix <> attr
42 changes: 42 additions & 0 deletions tests/smoke/Commands/html.smoke.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,45 @@ tests:
Copying assets files to .*
Writing .*HelloWorld.html
exit-status: 0

- name: html-only-code
command:
shell:
- bash
script: |
cd milestone/HelloWorld
rm -rf html
juvix html HelloWorld.juvix --only-source --only-code --non-recursive
cat html/HelloWorld.html
stdout:
matches: |
<span class="ju-comment">-- HelloWorld.juvix</span>.*
exit-status: 0

- name: html-id-prefix
command:
shell:
- bash
script: |
cd milestone/HelloWorld
rm -rf html
juvix html HelloWorld.juvix --only-source --only-code --prefix-id="XYZ"
cat html/HelloWorld.html
stdout:
matches: |
.*href="HelloWorld.html#XYZ[0-9]+".*
exit-status: 0

- name: html-no-path
command:
shell:
- bash
script: |
cd milestone/HelloWorld
rm -rf html
juvix html HelloWorld.juvix --only-source --only-code --no-path
cat html/HelloWorld.html
stdout:
matches: |
.*href="#[0-9]+".*
exit-status: 0
Loading