packages feed

mmark-ext 0.1.0.0 → 0.1.1.0

raw patch · 9 files changed

+281/−10 lines, 9 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ Text.MMark.Extension.Footnotes: footnotes :: Extension
+ Text.MMark.Extension.MathJax: mathJax :: Maybe Char -> Extension

Files

CHANGELOG.md view
@@ -1,3 +1,9 @@+## MMark Ext 0.1.1.0++* Added the `Text.MMark.Extension.MathJax` module.++* Added the `Text.MMark.Extension.Footnotes` module.+ ## MMark Ext 0.1.0.0  * This version has little in common with previous versions.
README.md view
@@ -4,8 +4,8 @@ [![Hackage](https://img.shields.io/hackage/v/mmark-ext.svg?style=flat)](https://hackage.haskell.org/package/mmark-ext) [![Stackage Nightly](http://stackage.org/package/mmark-ext/badge/nightly)](http://stackage.org/nightly/package/mmark-ext) [![Stackage LTS](http://stackage.org/package/mmark-ext/badge/lts)](http://stackage.org/lts/package/mmark-ext)-[![Build Status](https://travis-ci.org/mrkkrp/mmark-ext.svg?branch=master)](https://travis-ci.org/mrkkrp/mmark-ext)-[![Coverage Status](https://coveralls.io/repos/mrkkrp/mmark-ext/badge.svg?branch=master&service=github)](https://coveralls.io/github/mrkkrp/mmark-ext?branch=master)+[![Build Status](https://travis-ci.org/mmark-md/mmark-ext.svg?branch=master)](https://travis-ci.org/mmark-md/mmark-ext)+[![Coverage Status](https://coveralls.io/repos/mmark-md/mmark-ext/badge.svg?branch=master&service=github)](https://coveralls.io/github/mmark-md/mmark-ext?branch=master)  Commonly useful extensions for [MMark](https://hackage.haskell.org/package/mmark) markdown processor.@@ -13,7 +13,7 @@ ## Contribution  Issues, bugs, and questions may be reported in [the GitHub issue tracker for-this project](https://github.com/mrkkrp/mmark-ext/issues).+this project](https://github.com/mmark-md/mmark-ext/issues).  Pull requests are also welcome and will be reviewed quickly. 
Text/MMark/Extension/Common.hs view
@@ -47,8 +47,10 @@ module Text.MMark.Extension.Common   ( module Text.MMark.Extension.Comment   , module Text.MMark.Extension.FontAwesome+  , module Text.MMark.Extension.Footnotes   , module Text.MMark.Extension.Kbd   , module Text.MMark.Extension.LinkTarget+  , module Text.MMark.Extension.MathJax   , module Text.MMark.Extension.ObfuscateEmail   , module Text.MMark.Extension.PunctuationPrettifier   , module Text.MMark.Extension.Skylighting@@ -57,8 +59,10 @@  import Text.MMark.Extension.Comment import Text.MMark.Extension.FontAwesome+import Text.MMark.Extension.Footnotes import Text.MMark.Extension.Kbd import Text.MMark.Extension.LinkTarget+import Text.MMark.Extension.MathJax import Text.MMark.Extension.ObfuscateEmail import Text.MMark.Extension.PunctuationPrettifier import Text.MMark.Extension.Skylighting
+ Text/MMark/Extension/Footnotes.hs view
@@ -0,0 +1,108 @@+-- |+-- Module      :  Text.MMark.Extension.Footnotes+-- Copyright   :  © 2018 Mark Karpov+-- License     :  BSD 3 clause+--+-- Maintainer  :  Mark Karpov <markkarpov92@gmail.com>+-- Stability   :  experimental+-- Portability :  portable+--+-- An extension to add footnotes to your documents.+--+-- @since 0.1.1.0++{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE QuasiQuotes       #-}++module Text.MMark.Extension.Footnotes+  ( footnotes )+where++import Control.Monad+import Data.Char (isDigit)+import Data.List.NonEmpty (NonEmpty (..))+import Data.Semigroup ((<>))+import Data.Text (Text)+import Lens.Micro ((^.))+import Lucid+import Text.MMark.Extension (Extension, Inline (..), Block (..), getOis)+import Text.URI.Lens (uriPath)+import Text.URI.QQ (scheme)+import qualified Data.List.NonEmpty   as NE+import qualified Data.Text            as T+import qualified Text.MMark.Extension as Ext+import qualified Text.URI             as URI++-- | The extension performs two transformations:+--+--     * It turns links with URIs with @footnote@ scheme and single path+--       piece consisting of a number into links to footnote references.+--     * It turns block quotes with the @\"footnotes\"@ label (see the+--       example below) into a footnote section.+--+-- > Here goes some text [1](footnote:1).+-- >+-- > > footnotes+-- >+-- >   1. Here we have the footnote.+--+-- The extension is not fully safe though in the sense that we can't check+-- that a footnote reference refers to an existing footnote and that+-- footnotes have corresponding references, or that they are present in the+-- document in the right order.++footnotes :: Extension+footnotes = footnoteRefs <> footnoteSection++-- | Create footnote references.++footnoteRefs :: Extension+footnoteRefs = Ext.inlineRender $ \old inline ->+  case inline of+    l@(Link _ uri _) ->+      if URI.uriScheme uri == Just [scheme|footnote|]+        then case uri ^. uriPath of+               [x'] ->+                 let x = URI.unRText x'+                 in if T.all isDigit x+                      then a_ [ fragmentHref (footnoteId x)+                              , id_ (referenceId x) ] $+                             sup_ (toHtml x)+                      else old l+               _ -> old l+        else old l+    other -> old other++-- | Create footnote section.++footnoteSection :: Extension+footnoteSection = Ext.blockRender $ \old block ->+  case block of+    b@(Blockquote [Paragraph (pOis, _), OrderedList i items]) ->+      if getOis pOis == Plain "footnotes" :| []+        then do let startIndex = [start_ (renderIx i) | i /= 1]+                    renderIx   = T.pack . show+                ol_ startIndex $ do+                  newline+                  forM_ (NE.zip (NE.iterate (+ 1) i) items) $ \(j, x) -> do+                    let j' = renderIx j+                    li_ [id_ (footnoteId j')] $ do+                      newline+                      mapM_ old x+                      a_ [fragmentHref (referenceId j')] "↩"+                    newline+                newline++        else old b+    other -> old other+    where+      newline = "\n"++fragmentHref :: Text -> Attribute+fragmentHref = href_ . URI.render . Ext.headerFragment++footnoteId :: Text -> Text+footnoteId x = "fn" <> x++referenceId :: Text -> Text+referenceId x = "fnref" <> x
+ Text/MMark/Extension/MathJax.hs view
@@ -0,0 +1,79 @@+-- |+-- Module      :  Text.MMark.Extension.MathJax+-- Copyright   :  © 2018 Mark Karpov+-- License     :  BSD 3 clause+--+-- Maintainer  :  Mark Karpov <markkarpov92@gmail.com>+-- Stability   :  experimental+-- Portability :  portable+--+-- Turn code spans and fenced code blocks into MathJax formulas.+--+-- @since 0.1.1.0++{-# LANGUAGE OverloadedStrings #-}++module Text.MMark.Extension.MathJax+  ( mathJax )+where++import Control.Monad+import Data.Semigroup ((<>))+import Data.Text (Text)+import Lucid+import Text.MMark.Extension (Extension, Inline (..), Block (..))+import qualified Data.Text            as T+import qualified Text.MMark.Extension as Ext++-- | The extension allows to transform inline code spans into MathJax inline+-- spans and code blocks with the info string @\"mathjax\"@ (case-sensitive)+-- into MathJax display spans. Every line in such a code block will produce+-- a separate display span, i.e. a separate line with a formula (which is+-- probably what you want anyway).+--+-- The first argument is the character that must be the first and the last+-- character in code spans for them to be recognized as MathJax markup. If+-- 'Nothing' is passed instead of a char, we apply the transformation to all+-- code spans (useful for more academic articles that do not deal with+-- code).++mathJax+  :: Maybe Char -- ^ Starting\/ending character in MathJax inline spans+  -> Extension+mathJax mch = mathJaxSpan mch <> mathJaxBlock++-- | Turn code spans that start and end with a given character into MathJax+-- inline spans. If 'Nothing' is provided instead of a char, apply the+-- transformation to all code spans.++mathJaxSpan :: Maybe Char -> Extension+mathJaxSpan mch = Ext.inlineRender $ \old inline ->+  case inline of+    s@(CodeSpan txt) ->+      case mch of+        Nothing -> spn txt+        Just ch ->+          if T.length txt >= 2 && T.head txt == ch && T.last txt == ch+            then (spn . T.dropEnd 1 . T.drop 1) txt+            else old s+    other -> old other+  where+    spn :: Text -> Html ()+    spn x = span_ [class_ "math inline"] $+      "\\(" >> toHtml x >> "\\)"++-- | Turn code blocks with info string @\"mathjax\"@ into MathJax display+-- spans.++mathJaxBlock :: Extension+mathJaxBlock = Ext.blockRender $ \old block ->+  case block of+    b@(CodeBlock mlabel txt) ->+      if mlabel == Just "mathjax"+        then do+               p_ . forM_ (T.lines txt) $ \x ->+                 span_ [class_ "math display"] $+                   "\\[" >> toHtml x >> "\\]"+               "\n"+        else old b+    other -> old other
Text/MMark/Extension/Skylighting.hs view
@@ -43,6 +43,7 @@            Just syntax ->              case S.tokenize tokenizerConfig syntax txt of                Left _ -> old cb-               Right sourceLines -> toHtmlRaw . renderHtml $-                 S.formatHtmlBlock fmtOpts sourceLines+               Right sourceLines -> do+                 toHtmlRaw . renderHtml $ S.formatHtmlBlock fmtOpts sourceLines+                 "\n"     other -> old other
mmark-ext.cabal view
@@ -1,13 +1,13 @@ name:                 mmark-ext-version:              0.1.0.0+version:              0.1.1.0 cabal-version:        >= 1.18 tested-with:          GHC==8.0.2, GHC==8.2.2 license:              BSD3 license-file:         LICENSE.md author:               Mark Karpov <markkarpov92@gmail.com> maintainer:           Mark Karpov <markkarpov92@gmail.com>-homepage:             https://github.com/mrkkrp/mmark-ext-bug-reports:          https://github.com/mrkkrp/mmark-ext/issues+homepage:             https://github.com/mmark-md/mmark-ext+bug-reports:          https://github.com/mmark-md/mmark-ext/issues category:             Text synopsis:             Commonly useful extensions for MMark markdown processor build-type:           Simple@@ -23,7 +23,7 @@  source-repository head   type:               git-  location:           https://github.com/mrkkrp/mmark-ext.git+  location:           https://github.com/mmark-md/mmark-ext.git  flag dev   description:        Turn on development settings.@@ -38,15 +38,17 @@                     , microlens        >= 0.4   && < 0.5                     , mmark            >= 0.0.4 && <= 0.1                     , modern-uri       >= 0.1.1 && < 0.3-                    , skylighting      >= 0.5   && < 0.6+                    , skylighting      >= 0.5   && < 0.7                     , text             >= 0.2   && < 1.3   if !impl(ghc >= 8.0)     build-depends:    semigroups       == 0.18.*   exposed-modules:    Text.MMark.Extension.Common                     , Text.MMark.Extension.Comment                     , Text.MMark.Extension.FontAwesome+                    , Text.MMark.Extension.Footnotes                     , Text.MMark.Extension.Kbd                     , Text.MMark.Extension.LinkTarget+                    , Text.MMark.Extension.MathJax                     , Text.MMark.Extension.ObfuscateEmail                     , Text.MMark.Extension.PunctuationPrettifier                     , Text.MMark.Extension.Skylighting@@ -73,8 +75,10 @@     ghc-options:      -O2 -Wall   other-modules:      Text.MMark.Extension.CommentSpec                     , Text.MMark.Extension.FontAwesomeSpec+                    , Text.MMark.Extension.FootnotesSpec                     , Text.MMark.Extension.KbdSpec                     , Text.MMark.Extension.LinkTargetSpec+                    , Text.MMark.Extension.MathJaxSpec                     , Text.MMark.Extension.ObfuscateEmailSpec                     , Text.MMark.Extension.PunctuationPrettifierSpec                     , Text.MMark.Extension.TableOfContentsSpec
+ tests/Text/MMark/Extension/FootnotesSpec.hs view
@@ -0,0 +1,32 @@+{-# LANGUAGE OverloadedStrings #-}++module Text.MMark.Extension.FootnotesSpec (spec) where++import Test.Hspec+import Text.MMark.Extension.Footnotes+import Text.MMark.Extension.TestUtils++spec :: Spec+spec =+  describe "footnotes" $ do+    let to = withExt footnotes+    context "when link has no scheme" $+      it "has no effect" $+        "Link [link](1)." `to`+          "<p>Link <a href=\"1\">link</a>.</p>\n"+    context "when link has not \"footnote\" scheme" $+      it "has no effect" $+        "Link [link](https:1)" `to`+          "<p>Link <a href=\"https:1\">link</a></p>\n"+    context "when link has \"footnote\" scheme" $+      it "transforms the link correctly" $+        "Link [link](footnote:1)" `to`+          "<p>Link <a href=\"#fn1\" id=\"fnref1\"><sup>1</sup></a></p>\n"+    context "when block quotes are not formatted correctly" $+      it "has no effect" $+        "> blah" `to`+          "<blockquote>\n<p>blah</p>\n</blockquote>\n"+    context "when block quotes are formatted correctly" $+      it "transforms them into footnotes" $+        "> footnotes\n\n  1. Something.\n" `to`+          "<ol>\n<li id=\"fn1\">\nSomething.\n<a href=\"#fnref1\">↩</a></li>\n</ol>\n"
+ tests/Text/MMark/Extension/MathJaxSpec.hs view
@@ -0,0 +1,37 @@+{-# LANGUAGE OverloadedStrings #-}++module Text.MMark.Extension.MathJaxSpec (spec) where++import Test.Hspec+import Text.MMark.Extension.MathJax+import Text.MMark.Extension.TestUtils++spec :: Spec+spec =+  describe "mathJax" $ do+    let to  = withExt (mathJax Nothing)+        to' = withExt (mathJax (Just '$'))+    context "when span char is not specified" $+      it "transforms all code spans correctly" $+        "I've got `foo`." `to`+          "<p>I&#39;ve got <span class=\"math inline\">\\(foo\\)</span>.</p>\n"+    context "when span char is specified" $ do+      it "does not affect mismatching code spans" $+        "I've got `foo`." `to'`+          "<p>I&#39;ve got <code>foo</code>.</p>\n"+      it "transforms matching code spans correctly" $+        "I've got `$foo$`." `to'`+          "<p>I&#39;ve got <span class=\"math inline\">\\(foo\\)</span>.</p>\n"+    context "when code block is not labelled with \"mathjax\"" $+      it "does not affect it" $+        "```\nfoo\n```\n" `to`+          "<pre><code>foo\n</code></pre>\n"+    context "when code block is labelled with \"mathjax\"" $ do+      context "when code block contains a single line" $+        it "renders it correctly" $+          "```mathjax\nfoo\n```\n" `to`+            "<p><span class=\"math display\">\\[foo\\]</span></p>\n"+      context "when code block contains multiple lines" $+        it "renders it correctly" $+          "```mathjax\nfoo\nbar\n```\n" `to`+            "<p><span class=\"math display\">\\[foo\\]</span><span class=\"math display\">\\[bar\\]</span></p>\n"