-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Literate Juvix Markdown (#2448)
This PR adds an initial support for Literate Juvix Markdown files, files with the extension `.juvix.md`. Here is a small example of such a file: `Test.juvix.md`. <pre> # This is a heading Lorem ... ```juvix module Test; type A := a; fun : A -> A | _ := a; ``` Other text </pre> This initial support enables users to execute common commands such as typechecking, compilation, and HTML generation. Additionally, a new command called `markdown` has been introduced. This command replaces code blocks marked with the juvix attribute with their respective HTML output, much like the output we obtain when running `juvix html`. In this version, comments are ignored in the output, including judoc blocks. - We intend to use this new feature in combination with this Python plugin (https://github.com/anoma/juvix-mkdocs) to enhance our documentation site. https://github.com/anoma/juvix/assets/1428088/a0c17f36-3d76-42cc-a571-91f885866874 ## Future work Open as issues once this PR is merged, we can work on the following: - Support imports of Juvix Markdown modules (update the path resolver to support imports of Literate Markdown files) - Support (Judoc) comments in md Juvix blocks - Support Markdown in Judoc blocks - Update Text editor support, vscode extension and emacs mode (the highlighting info is a few characters off in the current state) - Closes #1839 - Closes #1719
- Loading branch information
1 parent
31f519b
commit bd16d3e
Showing
28 changed files
with
1,070 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,3 +90,4 @@ hie.yaml | |
/.shake/ | ||
/.benchmark-results/ | ||
docs/assets/** | ||
.repos |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module Commands.Markdown where | ||
|
||
import Commands.Base | ||
import Commands.Markdown.Options | ||
import Data.Text.IO qualified as Text | ||
import Juvix.Compiler.Backend.Markdown.Translation.FromTyped.Source | ||
import Juvix.Compiler.Backend.Markdown.Translation.FromTyped.Source qualified as MK | ||
import Juvix.Compiler.Concrete.Data.ScopedName qualified as S | ||
import Juvix.Compiler.Concrete.Language qualified as Concrete | ||
import Juvix.Compiler.Concrete.Pretty qualified as Concrete | ||
import Juvix.Compiler.Concrete.Translation.FromParsed.Analysis.Scoping qualified as Scoper | ||
import Juvix.Extra.Assets (writeAssets) | ||
|
||
runCommand :: | ||
(Members '[Embed IO, App] r) => | ||
MarkdownOptions -> | ||
Sem r () | ||
runCommand opts = do | ||
let inputFile = opts ^. markdownInputFile | ||
scopedM <- runPipeline inputFile upToScoping | ||
let m = head (scopedM ^. Scoper.resultModules) | ||
outputDir <- fromAppPathDir (opts ^. markdownOutputDir) | ||
md :: Text <- | ||
MK.fromJuvixMarkdown | ||
ProcessJuvixBlocksArgs | ||
{ _processJuvixBlocksArgsConcreteOpts = Concrete.defaultOptions, | ||
_processJuvixBlocksArgsUrlPrefix = opts ^. markdownUrlPrefix, | ||
_processJuvixBlocksArgsIdPrefix = | ||
opts ^. markdownIdPrefix, | ||
_processJuvixBlocksArgsNoPath = | ||
opts ^. markdownNoPath, | ||
_processJuvixBlocksArgsComments = scopedM ^. Scoper.comments, | ||
_processJuvixBlocksArgsModule = m, | ||
_processJuvixBlocksArgsOutputDir = outputDir | ||
} | ||
if | ||
| opts ^. markdownStdout -> liftIO . putStrLn $ md | ||
| otherwise -> do | ||
ensureDir outputDir | ||
when (opts ^. markdownWriteAssets) $ | ||
liftIO $ | ||
writeAssets outputDir | ||
|
||
let mdFile :: Path Rel File | ||
mdFile = | ||
relFile | ||
( Concrete.topModulePathToDottedPath | ||
(m ^. Concrete.modulePath . S.nameConcrete) | ||
<.> markdownFileExt | ||
) | ||
absPath :: Path Abs File | ||
absPath = outputDir <//> mdFile | ||
|
||
liftIO $ Text.writeFile (toFilePath absPath) md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
module Commands.Markdown.Options where | ||
|
||
import CommonOptions | ||
|
||
data MarkdownOptions = MarkdownOptions | ||
{ _markdownInputFile :: AppPath File, | ||
_markdownOutputDir :: AppPath Dir, | ||
_markdownUrlPrefix :: Text, | ||
_markdownIdPrefix :: Text, | ||
_markdownNoPath :: Bool, | ||
_markdownStdout :: Bool, | ||
_markdownWriteAssets :: Bool | ||
} | ||
deriving stock (Data) | ||
|
||
makeLenses ''MarkdownOptions | ||
|
||
parseJuvixMarkdown :: Parser MarkdownOptions | ||
parseJuvixMarkdown = do | ||
_markdownUrlPrefix :: Text <- | ||
strOption | ||
( value mempty | ||
<> long "prefix-url" | ||
<> help "Prefix used for inner Juvix hyperlinks" | ||
) | ||
_markdownIdPrefix :: Text <- | ||
strOption | ||
( value mempty | ||
<> long "prefix-id" | ||
<> showDefault | ||
<> help "Prefix used for HTML element IDs" | ||
) | ||
_markdownInputFile <- parseInputFile FileExtJuvixMarkdown | ||
_markdownOutputDir <- | ||
parseGenericOutputDir | ||
( value "markdown" | ||
<> showDefault | ||
<> help "Markdown output directory" | ||
<> action "directory" | ||
) | ||
_markdownNoPath <- | ||
switch | ||
( long "no-path" | ||
<> help "Do not include the path to the input file in the HTML id hyperlinks" | ||
) | ||
_markdownWriteAssets <- | ||
switch | ||
( long "write-assets" | ||
<> help "Write the CSS/JS assets to the output directory" | ||
) | ||
_markdownStdout <- | ||
switch | ||
( long "stdout" | ||
<> help "Write the output to stdout instead of a file" | ||
) | ||
pure MarkdownOptions {..} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.