Skip to content

Instantly share code, notes, and snippets.

View bpj's full-sized avatar

Benct Philip Jonsson bpj

View GitHub Profile
#!/usr/bin/env perl
#----------------------------------------------------------------------
# pandoc-small-caps.pl
#
# Pandoc filter to convert spans with class 'small-caps'
# (and optionally ~~Strikeout~~) to SmallCaps.
#
# pandoc -F pandoc-small-caps.pl [-M strikeout2smallcaps] ARGUMENTS...
#
@bpj
bpj / string2attrs.md
Created November 19, 2015 15:23
Extract HTML-like attributes from an arbitrary string with CSS-like shortcuts

Note: This documents the usage of both the perl and the python version. I have made the perl and python version work as alike as I could. In order to not favoritize either side hashes/dictionaries are called "associative arrays" and depicted as JSON objects in all their ugliness! :-)

The string2attrs function parses a string, e.g. a title string from a Pandoc Link or Image element, with attributes similar to those used with Pandoc Code, CodeBlock and Header elements (minus the braces) into an associative array of key--value pairs. Something like

"#foo .bar lang=la baz='biz buz'"

becomes

{"baz":"biz buz","id":"foo","lang":"la","class":"bar"}
@bpj
bpj / perl_and_pod_folding.vim
Last active December 11, 2015 15:55
A foldexpr function to fold both Pod and Perl code sensibly in the same file
" vim: set ft=vim et tw=80:
" A foldexpr function to fold both Pod and Perl code sensibly in the same
" file. It folds Pod according to its logical structure and code by
" indentation.
"
" Author: Benct Philip Jonsson <bpjonsson@gmail.com>
" Version: 2015-12-11-14 (YYYY-MM-DD-HH)
"
" It requires that the Pod blocks are well-behaved and start with `=pod` or
@bpj
bpj / pandoc-expand-simple.py
Last active January 6, 2016 19:29
Pandoc filter to wrap Span/Div in LaTeX based on classes
#!/usr/bin/env python
"""
Pandoc filter to wrap Span/Div in LaTeX based on classes
"""
import pandocfilters as pf
import re
begin_command = None
@bpj
bpj / pandoc-brace-spans.py
Created January 6, 2016 19:31
Pandoc filter to restore pre 1.16 behavior of wrapping Span contents in braces in LaTeX output
#!/usr/bin/env python
"""
Pandoc filter to restore pre 1.16 behavior of wrapping Span contents in braces in LaTeX output,
since I and maybe others have documents and/or filters relying on the old behavior
"""
import pandocfilters as pf
want_braces = None
open_brace = [pf.RawInline('tex', '{')]
@bpj
bpj / pandoc-collect-floats.pl
Last active April 8, 2016 11:12
Pandoc filters (pl and py) to collect all figures and tables at a specified place in a document
#!/usr/bin/env perl
=pod
Pandoc filter which emulate the LaTeX endfloat package by extracting all
elements which would be LaTeX floats (figures and tables) from a
document and putting them in div with the id "figures" or "tables"
respectively. You must mark the points in the document where you want
the floats to go with a paragraph containing *only* the text
"FiguresHere" or "TablesHere" -- exactly as written here in CamelCase --
Moved to https://github.com/bpj/perlsubst.vim
@bpj
bpj / pandoc-incl-excl.py
Last active May 18, 2016 20:38
Pandoc filter which keeps/removes divs/spans which have some class
#!/usr/bin/env python
"""
Pandoc filter which removes divs and/or spans according to whether they
have certain classes which are listed in metadata or not. There are four
relevant metadata entries:
- incl_div
- excl_div
- incl_span
- excl_span
@bpj
bpj / pandoc-sf-sc.py
Last active May 23, 2016 17:19
Pandoc filter which fakes sans and small-caps syntax by overloading link syntax
#!/usr/bin/env python
"""
Pandoc filter which fakes sans and small-caps syntax through
overloading link syntax. It wraps the text of links with the
pseudo-urls ``-sf`` and ``-sc`` in HTML spans with the classes
"sans" and "small-caps" or LaTeX ``\textsf{...}`` and ``\textsc{...}``
commands::
[this is sans text](-sf)
@bpj
bpj / pandoc-html-only.pl
Last active October 13, 2016 09:43
Pandoc filter to filter out stuff unless the output format is html/html4/html5
#!/usr/bin/env perl
# exclude spans/divs marked html/html4/html5 unless output format is html/html4/html5
use utf8;
use autodie 2.26;
use 5.010001;
use strict;
use warnings;
use warnings qw(FATAL utf8);