pandoc-stylefrommeta 0.1.0.1 → 0.1.1.0
raw patch · 4 files changed
+86/−24 lines, 4 files
Files
- Changelog.md +5/−0
- README.md +58/−0
- pandoc-stylefrommeta.cabal +4/−2
- styleFromMeta.hs +19/−22
+ Changelog.md view
@@ -0,0 +1,5 @@+### 0.1.1.0++- Added support for Pandoc 2.0.+- Dropped support for GHC older than 7.8 (pattern synonyms are required).+
+ README.md view
@@ -0,0 +1,58 @@+styleFromMeta+=============++Pandoc filter to apply styles found in the metadata of the document for various+objects.++Styling is supported for following types of objects:++- Standalone images+- Inline images+- Links+- Paragraphs (with restrictions, see below)++Styles are read from the metadata of the document: they may reside inside the+document or in a separate YAML file. For example++```yaml+ ---+ img_style :+ html : |+ <div style="clear: both; text-align: center; margin-bottom: 16px">+ <a href="$SRC$" style="margin-left: 10em;" alt="$ALT$">+ <img border="0" src="$SRC$" /></a></div>+ latex : |+ \begin{center}+ \includegraphics{$SRC$}+ \end{center}+ link_style :+ html : |+ <a href="$SRC$" style="margin-left: 1em; margin-right: 1em;">$ALT$</a>+ latex : |+ \href{$SRC$}{\colorbox{green}{$ALT$}}+ para_style :+ html : |+ <span style="display: block; margin-bottom: 16px;"></span>+ ...+```++declares styles *img\_style*, *link\_style* and *para\_style*. Their names+(except for the last) are arbitrarily chosen and may be referred from the+document, for example++```markdown+ + [$link_style$ *here*](http://example.com/)+```++Placeholders `$ALT$`, `$SRC$` and `$TITLE$` from style declarations are to be+replaced by concrete data found in the object declaration. In the last example+`*here*` corresponds to `$ALT$`, and `http://example.com/` corresponds to+`$SRC$`.++As soon as paragraphs do not have place where to put extra data, style+*para\_style* is applied to all paragraphs in the document. Currently only+transformation to a span block is supported. Any contents found between opening+and closing span tags are ignored: actual paragraph contents will be inserted+inside them.+
pandoc-stylefrommeta.cabal view
@@ -1,5 +1,5 @@ name: pandoc-stylefrommeta-version: 0.1.0.1+version: 0.1.1.0 synopsis: Pandoc filter to customize links, images and paragraphs description: Pandoc filter to customize links, images and paragraphs (with restrictions). Styles are read from the metadata of the document:@@ -8,9 +8,10 @@ homepage: http://github.com/lyokha/styleFromMeta license: BSD3 license-file: LICENSE+extra-source-files: Changelog.md README.md author: Alexey Radkov <alexey.radkov@gmail.com> maintainer: Alexey Radkov <alexey.radkov@gmail.com>-copyright: 2016 Alexey Radkov+copyright: 2016-2017 Alexey Radkov category: Text build-type: Simple cabal-version: >= 1.8@@ -22,5 +23,6 @@ , containers >= 0.2 , MissingH >= 1.0.0 , HaTeX >= 3.3+ extensions: CPP main-is: styleFromMeta.hs
styleFromMeta.hs view
@@ -1,27 +1,23 @@--- styleFromMeta.hs- {-# OPTIONS_HADDOCK prune, ignore-exports #-}-{-# LANGUAGE CPP, ViewPatterns, PatternGuards #-}-#if __GLASGOW_HASKELL__ >= 708-{-# LANGUAGE PatternSynonyms #-}-#endif+{-# LANGUAGE ViewPatterns, PatternGuards, PatternSynonyms #-} -import Text.Pandoc.JSON-import Text.Pandoc.Walk (walk)-import Text.Pandoc.Shared (stringify)-import Text.Pandoc.XML (escapeStringForXML)+import Text.Pandoc.JSON+import Text.Pandoc.Walk (walk)+import Text.Pandoc.Shared (stringify)+import Text.Pandoc.XML (escapeStringForXML) import qualified Data.Map as M-import Data.String.Utils (replace)-import Text.LaTeX.Base.Syntax (protectString)+import Data.String.Utils (replace)+import Text.LaTeX.Base.Syntax (protectString) -#if __GLASGOW_HASKELL__ >= 708-pattern Style x <- Math InlineMath x-pattern Alt x <- (dropWhile (==Space) -> x)+#if MIN_VERSION_pandoc(2,0,0)+#define MBPLAIN Plain #else-#define Style Math InlineMath-#define Alt(x) (dropWhile (==Space) -> x)+#define MBPLAIN Para #endif +pattern Style x <- Math InlineMath x+pattern Alt x <- (dropWhile (== Space) -> x)+ type MMap = M.Map String MetaValue type PureInlineParams = ([Inline], Target) -- style:(alt, target) type InlineParams = (Inline, [Inline], Target) -- (style:alt, target)@@ -86,7 +82,7 @@ styleFromMeta _ p = return p substStyle :: Format -> MMap -> Block -> Block-substStyle fm@(Format fmt) m b@(Para [Image attr (Style style : Alt (alt)) tgt])+substStyle fm@(Format fmt) m b@(Para [Image attr (Style style : Alt alt) tgt]) | Just (MetaMap mm) <- M.lookup style m = let params = (alt, tgt) substStyle' (Just (MetaBlocks [RawBlock f s])) =@@ -101,7 +97,7 @@ | otherwise = b substStyle fm@(Format fmt) m (Para cnt) | Just (MetaMap mm) <- M.lookup "para_style" m- , Just (MetaBlocks [Para [Span attr _]]) <- M.lookup fmt mm =+ , Just (MetaBlocks [MBPLAIN [Span attr _]]) <- M.lookup fmt mm = walk (substInlineStyle fm m) $ Plain [Span attr cnt] substStyle fm m b = walk (substInlineStyle fm m) b @@ -109,7 +105,8 @@ substInlineStyle fm@(Format fmt) m i@(toInlineParams -> Just ((Style style, alt, tgt), cons)) | Just (MetaMap mm) <- M.lookup style m =- let substInlineStyle' (Just (MetaBlocks [Para (RawInline f s : r)])) =+ let substInlineStyle' (Just (MetaBlocks+ [MBPLAIN (RawInline f s : r)])) = RawInline f $ substParams fm params $ s ++ stringify' fm (map subst r) where params = (alt, tgt)@@ -121,9 +118,9 @@ substInlineStyle _ _ i = i toInlineParams :: Inline -> Maybe (InlineParams, InlineCons)-toInlineParams (Image attr (style@(Style _) : Alt (alt)) tgt) =+toInlineParams (Image attr (style@(Style _) : Alt alt) tgt) = Just ((style, alt, tgt), Image attr)-toInlineParams (Link attr (style@(Style _) : Alt (alt)) tgt) =+toInlineParams (Link attr (style@(Style _) : Alt alt) tgt) = Just ((style, alt, tgt), Link attr) toInlineParams _ = Nothing