Skip to content

Commit

Permalink
Add source
Browse files Browse the repository at this point in the history
  • Loading branch information
jez committed Nov 3, 2016
1 parent c8e3c0f commit 9263cfa
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 9 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Jacob Zimmerman

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

> Convert Pandoc Markdown-style footnotes into sidenotes
This is a simple Pandoc filter to convert footnotes into a format that can be
consumed by [Tufte CSS].
This is a simple [Pandoc filter] to convert footnotes into a format that can be
consumed by [Tufte CSS]. On the whole, this project weighs in at well under 100
lines of code. Check out [Main.hs](src/Main.hs) if you're curious how it works.

It's used by calling `pandoc --filter pandoc-sidenote`. To see it in action, see
[Tufte Pandoc CSS], a project which uses it. In particular, take a look at the
Makefile included in that project.

## Installation

Expand Down Expand Up @@ -31,13 +36,6 @@ stack build
stack install
```

## Usage

This is a [Pandoc filter]. It's used by calling `pandoc --filter pandoc-sidenote`.

To see it in action, see [Tufte Pandoc CSS], a project which uses it. In
particular, take a look at the Makefile included in that project.

## License

[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://jez.io/MIT-LICENSE.txt)
Expand Down
2 changes: 2 additions & 0 deletions Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
28 changes: 28 additions & 0 deletions pandoc-sidenote.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: pandoc-sidenote
version: 0.8.0.0
synopsis: Convert Pandoc Markdown-style footnotes into sidenotes
description: Convert Pandoc Markdown-style footnotes into sidenotes
homepage: https://github.com/jez/pandoc-sidenote#readme
license: MIT
license-file: LICENSE
author: Jake Zimmerman
maintainer: zimmerman.jake@gmail.com
copyright: 2016 Jake Zimmerman
category: CommandLine
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10

executable pandoc-sidenote
hs-source-dirs: src
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends: base
, monad-gen
, pandoc
, pandoc-types
default-language: Haskell2010

source-repository head
type: git
location: https://github.com/jez/pandoc-sidenote
73 changes: 73 additions & 0 deletions src/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{-# LANGUAGE FlexibleContexts #-}

module Main where

import Data.List (intercalate)
import Data.Maybe (listToMaybe)

import Control.Monad.Gen

import Text.Pandoc.Walk (Walkable, walk, walkM)
import Text.Pandoc.JSON

getFirstStr :: [Inline] -> Maybe String
getFirstStr [] = Nothing
getFirstStr (Str text : _) = Just text
getFirstStr (_ : inlines) = getFirstStr inlines

newline :: [Inline]
newline = [LineBreak, LineBreak]

-- Extract inlines from blocks
coerceToInline :: [Block] -> [Inline]
coerceToInline = concatMap deBlock . walk deNote
where deBlock :: Block -> [Inline]
deBlock (Plain ls) = ls
-- Simulate paragraphs with double LineBreak
deBlock (Para ls) = ls ++ newline
-- See extension: line_blocks
deBlock (LineBlock lss) = intercalate [LineBreak] lss ++ newline
-- Pretend RawBlock is RawInline (might not work!)
-- Consider: raw <div> now inside RawInline... what happens?
deBlock (RawBlock fmt str) = [RawInline fmt str]
-- lists, blockquotes, headers, hrs, and tables are all omitted
-- Think they shouldn't be? I'm open to sensible PR's.
deBlock _ = []

deNote (Note _) = Str ""
deNote x = x

filterInline :: Inline -> Gen Int Inline
filterInline (Note blocks) = do
-- Generate a unique number for the 'for=' attribute
i <- gen

-- Note has a [Block], but RawInline needs [Inline]
let content = coerceToInline blocks

-- The '{-}' symbol differentiates between margin note and side note
let nonu = getFirstStr content == Just "{-}"
let content' = if nonu then tail content else content

let labelCls = "margin-toggle" ++ (if nonu then "" else " sidenote-number")
let labelSym = if nonu then "&#8853;" else ""
let labelHTML = "<label for=\"sn-" ++ show i ++ "\" class=\"" ++ labelCls ++ "\">" ++ labelSym ++ "</label>"
let label = RawInline (Format "html") labelHTML

let inputHTML = "<input type=\"checkbox\" id=\"sn-" ++ show i ++ "\" " ++ "class=\"margin-toggle\"/>"
let input = RawInline (Format "html") inputHTML

let (ident, _, attrs) = nullAttr
let noteTypeCls = if nonu then "marginnote" else "sidenote"
let note = Span (ident, [noteTypeCls], attrs) content'

return $ Span nullAttr [label, input, note]

filterInline inline = return inline

usingSideNotes :: Pandoc -> Pandoc
usingSideNotes (Pandoc meta blocks) = Pandoc meta (runGen (walkM filterInline blocks))


main :: IO ()
main = toJSONFilter usingSideNotes
10 changes: 10 additions & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
flags: {}
extra-package-dbs: []
packages:
- '.'
extra-deps:
- doctemplates-0.1.0.2
- monad-gen-0.3.0.1
- pandoc-1.18
- pandoc-types-1.17
resolver: lts-7.7

0 comments on commit 9263cfa

Please sign in to comment.