-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New module Commonmark.Extensions.Citations. [API change] Inlines: change type of bracketedSuffix so the constructor operators on [Chunk il] rather than il. Export unChunks, which will now be needed to form the constructor. This is intended to give the constructor access to information only available in the Chunks, e.g. whether a semicolon was escaped. [API change] Also we now export Chunk from Comonmark.Inlines [API change].
- Loading branch information
Showing
8 changed files
with
136 additions
and
21 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
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
98 changes: 98 additions & 0 deletions
98
commonmark-extensions/src/Commonmark/Extensions/Citations.hs
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,98 @@ | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE UndecidableInstances #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TupleSections #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
module Commonmark.Extensions.Citations | ||
( HasCitations(..), | ||
citationsSpec | ||
) | ||
where | ||
import Commonmark.Types | ||
import Commonmark.Tokens | ||
import Commonmark.Syntax | ||
import Commonmark.Inlines | ||
import Commonmark.SourceMap ( addName, WithSourceMap, runWithSourceMap ) | ||
import Commonmark.Html ( htmlInline, addAttribute, Html(..), ElementType(..) ) | ||
import Commonmark.TokParsers | ||
import Control.Monad (guard) | ||
import Data.Text (Text) | ||
import Text.Parsec | ||
|
||
class HasCitations il where | ||
citationGroup :: il -> il -- we'll parse further in pandoc | ||
citation :: Text -> Bool -> il | ||
hasCitations :: il -> Bool | ||
|
||
instance HasCitations (Html il) where | ||
citationGroup = | ||
addAttribute ("class", "citation-group") . | ||
htmlInline "span" . | ||
Just | ||
citation ident suppressAuthor = | ||
addAttribute ("class", "citation") . | ||
addAttribute ("identifier", ident) . | ||
(if suppressAuthor | ||
then addAttribute ("suppress-author", "true") | ||
else id) $ | ||
htmlInline "span" Nothing | ||
hasCitations x = case x of | ||
HtmlElement InlineElement "span" attr _ -> | ||
lookup "class" attr == Just "citation" | ||
HtmlConcat w v -> hasCitations w || hasCitations v | ||
_ -> False | ||
|
||
instance (HasCitations il, Monoid il, Show il) => HasCitations (WithSourceMap il) where | ||
citationGroup x = (citationGroup <$> x) <* addName "citation" | ||
citation ident suppressAuthor = | ||
pure (citation ident suppressAuthor) <* addName "citation" | ||
hasCitations x = | ||
let (x', _) = runWithSourceMap x | ||
in hasCitations x' | ||
|
||
citationsSpec | ||
:: forall m bl il . (Monad m , IsInline il , IsBlock il bl, HasCitations il) | ||
=> SyntaxSpec m il bl | ||
citationsSpec = | ||
defaultSyntaxSpec { | ||
syntaxBracketedSpecs = [citationBracketedSpec] | ||
, syntaxInlineParsers = [withAttributes parseBareCitation] } | ||
|
||
where | ||
|
||
citationBracketedSpec :: BracketedSpec il | ||
citationBracketedSpec = BracketedSpec | ||
{ bracketedName = "Citation" | ||
, bracketedNests = True | ||
, bracketedPrefix = Nothing | ||
, bracketedSuffixEnd = Just ';' | ||
-- causes ; to be parsed in own chunk | ||
, bracketedSuffix = checkCitation | ||
} | ||
|
||
checkCitation _rm chunksInside = do | ||
-- if there are bare citations inside, return citation | ||
let chunkHasCitations c = | ||
case chunkType c of | ||
Parsed x -> hasCitations x | ||
_ -> False | ||
guard $ any chunkHasCitations chunksInside | ||
return $! citationGroup | ||
|
||
parseBareCitation :: (Monad m, HasCitations il, IsInline il) | ||
=> InlineParser m il | ||
parseBareCitation = do | ||
suppressAuthor <- (True <$ symbol '-') <|> pure False | ||
ident <- parseCitationId | ||
return $! citation ident suppressAuthor | ||
|
||
parseCitationId :: Monad m => InlineParser m Text | ||
parseCitationId = try $ do | ||
symbol '@' | ||
untokenize <$> | ||
(many1 (satisfyTok (\t -> case tokType t of | ||
Symbol c -> c `elem` ['_'] | ||
WordChars -> True | ||
_ -> False))) |
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
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