You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
<!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"><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.html"><input id="search-query" name="q" type="text" placeholder="Search docs"/></form><ul><li><a class="toctext" href="index.html"><strong>1.</strong> Introduction</a></li><li><a class="toctext" href="fileformat.html"><strong>2.</strong> File Format</a></li><li><a class="toctext" href="pipeline.html"><strong>3.</strong> Processing pipeline</a></li><li><a class="toctext" href="outputformats.html"><strong>4.</strong> Output Formats</a></li><li class="current"><a class="toctext" href="customprocessing.html"><strong>5.</strong> Custom pre- and post-processing</a><ul class="internal"></ul></li><li><a class="toctext" href="documenter.html"><strong>6.</strong> Interaction with Documenter.jl</a></li><li><a class="toctext" href="generated/example.html"><strong>7.</strong> Example</a></li></ul></nav><article id="docs"><header><nav><ul><li><a href="customprocessing.html"><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.html#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.html#Literate.markdown"><code>Literate.markdown</code></a>, <a href="outputformats.html#Literate.notebook"><code>Literate.notebook</code></a> and <a href="outputformats.html#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.html#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><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><footer><hr/><a class="previous" href="outputformats.html"><span class="direction">Previous</span><span class="title"><strong>4.</strong> Output Formats</span></a><a class="next" href="documenter.html"><span class="direction">Next</span><span class="title"><strong>6.</strong> Interaction with Documenter.jl</span></a></footer></article></body></html>
|
|
|
|