• Stars
    star
    140
  • Rank 261,473 (Top 6 %)
  • Language
    Haskell
  • License
    Mozilla Public Li...
  • Created almost 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Pandoc filter for including code from source files

pandoc-include-code

A Pandoc filter for including code from source files.

You get to:

  • Keep your examples and documentation compiled and in sync
  • Include small snippets from larger source files without needing to keep track of line numbers
  • Dedent included snippets

Usage

The filter recognizes code blocks with the include attribute present. It swaps the content of the code block with contents from a file.

Including Files

The simplest way to use this filter is to include an entire file:

```{include=docs/MyFile.hs}
```

You can still use other attributes, and classes, to control the code blocks:

```{.purescript include=docs/MyFile.purs}
```

Snippets

There is support for delimited snippets. Use a line comment of whatever kind you want, and enclose the snippet between start snippet <name> and end snippet <name>.

-- start snippet cool-thingy
main =
  putStrLn "I explain some cool concept in Haskell code."
-- end snippet cool-thingy

Or why not some C code:

// start snippet wow
int main() {
    printf("such performance");
}
// end snippet wow

NOTE: There can only be whitespace and a newline after the snippet name. This means that multi-line comments in C, Java, etc, will not work. Only single-line comments will.

Then, in your code block, specify the snippet name:

```{include=docs/MyFile.hs snippet=cool-thingy}
```

Ranges

If you want to include a specific range of lines, use startLine and endLine:

```{include=docs/MyFile.hs startLine=35 endLine=80}
```

Dedent

Using the dedent attribute, you can have whitespaces removed on each line, where possible (non-whitespace character will not be removed even if they occur in the dedent area).

```{include=docs/MyFile.hs dedent=4}
```

Conflicting Modes

"Snippet mode" and "range mode" cannot be used together.

Line Numbers

If you include the numberLines class in your code block, and use include, the startFrom attribute will be added with respect to the included code's location in the source file.

```{include=docs/MyFile.hs startLine=35 endLine=80 .numberLines}
```

Adding Direct Links

It is possible to add hyperlinks to the original source code file specified in the include attribute by adding the .includeLink class in your code block.

```{include=docs/MyFile.hs .includeLink}
```

Adding Base url for all CodeBlock links

A base url will be appended to all relative paths specified in the include attribute of each CodeBlock . It does not affect paths beginning with file: , C:, \, /,.... This can be done two option:

Option 1: YAML

Specify a base key along with the base url as the attribute in the YAML header:

    ---
    title:    All About Wonderland
    author:   Alice
    date:     November 2020
    base:     http://localhost:8000/
    ---

Option 2: Command Line

Add the base as a metavalue -M base=<base url> or --metavalue base=<base url> in the command line when calling pandoc.

pandoc --filter pandoc-include-code -M base=http://localhost:8000/  in.md -o out.html

NOTE: If the base url is specified in the metadata block, then by specifying a different base in the command line, it will override the original base.

Both of these options will add a hyperlink to the filepath defined in the include attribute linking to http://localhost:8000/source/sample.hs:

Overriding a specified base url

Adding a base attribute in the metadata block or the command line will affect all relative links in the CodeBlocks. To add an alternative base for a specific link, add the base as an attribute base=https.... to theCodeBlock:

```{.haskell .includeLink include=source/sample.hs snippet=animals base=<path>}
```

This adds a hyperlink to the filepath specified in the include attribute linking to ../source/sample.hs :

More Usage Examples

Install

Executables for Linux and macOS are available in the Releases page.

From Homebrew

You can use Homebrew to install this filter:

brew install pandoc-include-code

From Hackage

If you'd rather install using cabal or stack, you can use the following command:

cabal install pandoc-include-code

The package is available at Hackage.

Build

Requirements:

To install from sources, run:

git clone [email protected]:owickstrom/pandoc-include-code.git
cd pandoc-include-code
cabal configure
cabal install

Run

If you have installed from sources, and you have ~/.local/bin on your PATH, you can use the filter with Pandoc like so:

pandoc --filter pandoc-include-code input.md output.html

Usage with Hakyll

If you are using the Hakyll static site generator, you can use the filter by importing it as a library and using the snippet below.

Add pandoc, pandoc-types, and pandoc-include-code to your project dependencies, then define a custom Hakyll compiler using a Pandoc transform:

import Text.Pandoc (Format (..), Pandoc)
import Text.Pandoc.Walk (walkM)
import Text.Pandoc.Filter.IncludeCode (includeCode)

includeCodeTransform :: Pandoc -> IO Pandoc
includeCodeTransform = walkM (includeCode (Just (Format "html5")))

includeCodePandocCompiler :: Compiler (Item String)
includeCodePandocCompiler =
  pandocCompilerWithTransformM
    defaultHakyllReaderOptions
    defaultHakyllWriterOptions
    (unsafeCompiler . includeCodeTransform)

You can now use includeCodePandocCompiler instead of the default pandocCompiler in your Hakyll rules:

match "*.md" $ do
  route $ setExtension "html"
  compile $ includeCodePandocCompiler
    >>= loadAndApplyTemplate "templates/default.html" defaultContext
    >>= relativizeUrls

Changelog

CHANGELOG.md

License

Mozilla Public License Version 2.0

More Repositories

1

the-monospace-web

A minimalist design exploration
HTML
1,258
star
2

komposition

The video editor built for screencasters
Haskell
429
star
3

gi-gtk-declarative

Declarative GTK+ programming in Haskell
Haskell
288
star
4

vim-colors-paramount

A minimal colorscheme for Vim that only puts emphasis on the paramount.
Vim Script
243
star
5

idris-vimscript

Compile Idris to Vimscript, like you always wanted.
Haskell
130
star
6

motor

Type-safe effectful state machines in Haskell
Haskell
94
star
7

tlaplus-cli-template

CLI-driven TLA+ project template
Makefile
43
star
8

pandoc-emphasize-code

A Pandoc filter for emphasizing code in fenced blocks
Haskell
28
star
9

minimal-kotlin-nix-example

Minimal Kotlin and Nix flake example (with Gradle 8 support)
Nix
26
star
10

automating-the-build-of-your-technical-presentation-template

The full template for "Automating the Build of your Technical Presentation"
TeX
26
star
11

wickstrom.tech

Source for my blog
HTML
24
star
12

fast-and-fearless-evolution-of-server-side-webapps

Slides and code examples for a talk
Haskell
22
star
13

purescript-leffe

The Labeled Effects Extension to PureScript
PureScript
21
star
14

twitter-kinesis-lab

A lab on how to use Amazon Kinesis for finding popular hashtags.
Clojure
20
star
15

dotfiles

My dotfiles for OSX and Ubuntu
Vim Script
19
star
16

minimal-beamer-slides-template

A minimal template for LaTeX Beamer slideshows
TeX
19
star
17

smug

The Sheet Music Generator
Clojure
14
star
18

domain-modeling-with-haskell-data-structures

A talk given at the Polyglot meetup in CPH, March 2018
Haskell
13
star
19

home-manager

Nix home-manager setup
Nix
8
star
20

fsm-your-compiler-wants-in

The slide sources for "Finite-state machines? Your compiler wants in!"
Haskell
7
star
21

hedgehog-inline-java-testing

haskell-hedgehog's model-based state machine testing together with inline-java
Haskell
6
star
22

sqsd-local

A local version of sqsd, the daemon in Elastic Beanstalk's Worker Environments.
Haskell
6
star
23

declarative-gtk-programming-in-haskell

A talk given at LambdAle
JavaScript
5
star
24

writing-a-screencast-video-editor-in-haskell

A talk for Lambda World Cadiz 2018
JavaScript
4
star
25

gameoflife

Conway's Game Of Life, written in ClojureScript.
Clojure
3
star
26

websub

A WebSub client for Haskell
Haskell
3
star
27

.emacs.d

Emacs Lisp
3
star
28

domain-modeling-with-haskell-data-structures-oredev

JavaScript
2
star
29

property-based-testing-the-ugly-parts

A talk for MF#K, February 26, 2019
JavaScript
2
star
30

fuzzig

Melding fuzzing and PBT concepts in Zig (experiment)
Zig
2
star
31

domain-modelling-with-haskell-workshop

HTML
2
star
32

power-of-fp-static-types-server-side-web

Slides of my talk: The Power of Functional Programming and Static Type Systems in Server-Side Web Applications
TeX
2
star
33

owickstrom

The profile page
1
star
34

clojurescript-koans

Clojure Koans for ClojureScript
Clojure
1
star
35

zap

A LISP interpreter in Javascript
JavaScript
1
star
36

haskell-robot-lab

An introductory Haskell lab.
Haskell
1
star
37

specifying-and-testing-web-applications

Slides for a talk
TeX
1
star
38

hello-node-cljs

Testing Node and Express with ClojureScript.
Clojure
1
star
39

gitpod-nix-test

Dockerfile
1
star
40

service-worker-lab

Add offline capabilities to a web app using Service Worker.
JavaScript
1
star
41

guism-example

WIP
Haskell
1
star
42

blog-post-dataflow

Makefile
1
star
43

psc-vimscript

PureScript
1
star