-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
141 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import Distribution.Simple | ||
main = defaultMain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "⊕" 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |