|
|
<!DOCTYPE html> |
|
|
<html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>5. Custom pre- and post-processing · Literate.jl</title><link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.2.0/normalize.min.css" rel="stylesheet" type="text/css"/><link href="https://fonts.googleapis.com/css?family=Lato|Roboto+Mono" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css" rel="stylesheet" type="text/css"/><script>documenterBaseURL=".."</script><script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.2.0/require.min.js" data-main="../assets/documenter.js"></script><script src="../siteinfo.js"></script><script src="../../versions.js"></script><link href="../assets/documenter.css" rel="stylesheet" type="text/css"/></head><body><nav class="toc"><a href="../index.html"><img class="logo" src="../assets/logo.png" alt="Literate.jl logo"/></a><h1>Literate.jl</h1><select id="version-selector" onChange="window.location.href=this.value" style="visibility: hidden"></select><form class="search" id="search-form" action="../search/"><input id="search-query" name="q" type="text" placeholder="Search docs"/></form><ul><li><a class="toctext" href="../"><strong>1.</strong> Introduction</a></li><li><a class="toctext" href="../fileformat/"><strong>2.</strong> File Format</a></li><li><a class="toctext" href="../pipeline/"><strong>3.</strong> Processing pipeline</a></li><li><a class="toctext" href="../outputformats/"><strong>4.</strong> Output Formats</a></li><li class="current"><a class="toctext" href><strong>5.</strong> Custom pre- and post-processing</a><ul class="internal"></ul></li><li><a class="toctext" href="../documenter/"><strong>6.</strong> Interaction with Documenter.jl</a></li><li><a class="toctext" href="../generated/example/"><strong>7.</strong> Example</a></li></ul></nav><article id="docs"><header><nav><ul><li><a href><strong>5.</strong> Custom pre- and post-processing</a></li></ul><a class="edit-page" href="https://github.com/fredrikekre/Literate.jl/blob/master/docs/src/customprocessing.md"><span class="fa"></span> Edit on GitHub</a></nav><hr/><div id="topbar"><span>5. Custom pre- and post-processing</span><a class="fa fa-bars" href="#"></a></div></header><h1><a class="nav-anchor" id="Custom-pre-and-post-processing-1" href="#Custom-pre-and-post-processing-1"><strong>5.</strong> Custom pre- and post-processing</a></h1><p>Since all packages are different, and may have different demands on how to create a nice example for the documentation it is important that the package maintainer does not feel limited by the by default provided syntax that this package offers. While you can generally come a long way by utilizing <a href="../fileformat/#Filtering-Lines-1">line filtering</a> there might be situations where you need to manually hook into the generation and change things. In Literate this is done by letting the user supply custom pre- and post-processing functions that may do transformation of the content.</p><p>All of the generators (<a href="../outputformats/#Literate.markdown"><code>Literate.markdown</code></a>, <a href="../outputformats/#Literate.notebook"><code>Literate.notebook</code></a> and <a href="../outputformats/#Literate.script"><code>Literate.script</code></a>) accepts <code>preprocess</code> and <code>postprocess</code> keyword arguments. The default "transformation" is the <code>identity</code> function. The input to the transformation functions is a <code>String</code>, and the output should be the transformed <code>String</code>.</p><p><code>preprocess</code> is sent the raw input that is read from the source file (<a href="../pipeline/#Pre-processing-1">modulo the default line ending transformation</a>). <code>postprocess</code> is given different things depending on the output: For markdown and script output <code>postprocess</code> is given the content <code>String</code> just before writing it to the output file, but for notebook output <code>postprocess</code> is given the dictionary representing the notebook, since, in general, this is more useful.</p><h3><a class="nav-anchor" id="Example:-Adding-current-date-1" href="#Example:-Adding-current-date-1">Example: Adding current date</a></h3><p>As an example, lets say we want to splice the date of generation into the output. We could of course update our source file before generating the docs, but we could instead use a <code>preprocess</code> function that splices the date into the source for us. Consider the following source file:</p><pre><code class="language-julia"># # Example |
|
|
# This example was generated DATEOFTODAY |
|
|
|
|
|
x = 1 // 3</code></pre><p>where <code>DATEOFTODAY</code> is a placeholder, to make it easier for our <code>preprocess</code> function to find the location. Now, lets define the <code>preprocess</code> function, for example</p><pre><code class="language-julia">function update_date(content) |
|
|
content = replace(content, "DATEOFTODAY" => Date(now())) |
|
|
return content |
|
|
end</code></pre><p>which would replace every occurrence of <code>"DATEOFTODAY"</code> with the current date. We would now simply give this function to the generator, for example:</p><pre><code class="language-julia">Literate.markdown("input.jl", "outputdir"; preprocess = update_date)</code></pre><h3><a class="nav-anchor" id="Example:-Replacing-include-calls-with-included-code-1" href="#Example:-Replacing-include-calls-with-included-code-1">Example: Replacing <code>include</code> calls with included code</a></h3><p>Let's say that we have some individual example files <code>file1, file2, ...</code> etc. that are <em>runnable</em> and also following the style of Literate. These files could be for example used in the test suite of your package.</p><p>We want to group them all into a single page in our documentation, but we do not want to copy paste the content of <code>file1, ...</code> for robustness: the files are included in the test suite and some changes may occur to them. We want these changes to also be reflected in the documentation.</p><p>A very easy way to do this is using <code>preprocess</code> to interchange <code>include</code> statements with file content. First, create a runnable <code>.jl</code> following the format of Literate</p><pre><code class="language-julia"># # Replace includes |
|
|
# This is an example to replace `include` calls with the actual file content. |
|
|
|
|
|
include("file1.jl") |
|
|
|
|
|
# Cool, we just saw the result of the above code snippet. Here is one more: |
|
|
|
|
|
include("file2.jl")</code></pre><p>Let's say we have saved this file as <code>examples.jl</code>. Then, you want to properly define a pre-processing function:</p><pre><code class="language-julia">function replace_includes(str) |
|
|
|
|
|
included = ["file1.jl", "file2.jl"] |
|
|
|
|
|
# Here the path loads the files from their proper directory, |
|
|
# which may not be the directory of the `examples.jl` file! |
|
|
path = "directory/to/example/files/" |
|
|
|
|
|
for ex in included |
|
|
content = read(path*ex, String) |
|
|
str = replace(str, "include(\"$(ex)\")" => content) |
|
|
end |
|
|
return str |
|
|
end</code></pre><p>(of course replace <code>included</code> with your respective files)</p><p>Finally, you simply pass this function to e.g. <a href="../outputformats/#Literate.markdown"><code>Literate.markdown</code></a> as</p><pre><code class="language-julia">Literate.markdown("examples.jl", "path/to/save/markdown"; |
|
|
name = "markdown_file_name", preprocess = replace_includes)</code></pre><p>and you will see that in the final output file (here <code>markdown_file_name.md</code>) the <code>include</code> statements are replaced with the actual code to be included!</p><p>This approach is used for example in the documentation of the Julia package <a href="https://github.com/JuliaDynamics/TimeseriesPrediction.jl"><code>TimeseriesPrediction</code></a>, see <a href="https://github.com/JuliaDynamics/DynamicalSystems.jl/blob/master/docs/src/tsprediction/stexamples.jl">here</a> and <a href="https://github.com/JuliaDynamics/DynamicalSystems.jl/blob/master/docs/make.jl#L11-L29">here for the generating script</a></p><footer><hr/><a class="previous" href="../outputformats/"><span class="direction">Previous</span><span class="title"><strong>4.</strong> Output Formats</span></a><a class="next" href="../documenter/"><span class="direction">Next</span><span class="title"><strong>6.</strong> Interaction with Documenter.jl</span></a></footer></article></body></html>
|
|
|
|