Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Text/Pandoc/Arbitrary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ instance Arbitrary Inline where
shrink (Image attr ils target) = [Image attr ils' target | ils' <- shrinkInlineList ils]
++ [Image attr ils target' | target' <- shrink target]
++ [Image attr' ils target | attr' <- shrink attr]
shrink (Note blks) = Note <$> shrinkBlockList blks
shrink (Note nt blks) = Note nt <$> shrinkBlockList blks
shrink (Span attr s) = (Span attr <$> shrink s)
++ (flip Span s <$> shrink attr)

Expand Down Expand Up @@ -142,7 +142,7 @@ arbInline n = frequency $ [ (60, Str <$> realString)
, (10, Link <$> arbAttr <*> arbInlines (n-1) <*> ((,) <$> realString <*> realString))
, (10, Image <$> arbAttr <*> arbInlines (n-1) <*> ((,) <$> realString <*> realString))
, (2, Cite <$> arbitrary <*> arbInlines 1)
, (2, Note <$> resize 3 (listOf1 $ arbBlock (n-1)))
, (2, Note <$> arbitrary <*> resize 3 (listOf1 $ arbBlock (n-1)))
]

instance Arbitrary Block where
Expand Down Expand Up @@ -261,6 +261,14 @@ instance Arbitrary QuoteType where
1 -> return DoubleQuote
_ -> error "FATAL ERROR: Arbitrary instance, logic bug"

instance Arbitrary NoteType where
arbitrary
= do x <- choose (0 :: Int, 1)
case x of
0 -> return Footnote
1 -> return Endnote
_ -> error "FATAL ERROR: Arbitrary instance, logic bug"

instance Arbitrary Meta where
arbitrary
= do (x1 :: Inlines) <- arbitrary
Expand Down
6 changes: 5 additions & 1 deletion Text/Pandoc/Builder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ module Text.Pandoc.Builder ( module Text.Pandoc.Definition
, image
, imageWith
, note
, endNote
, spanWith
, trimInlines
-- * Block list builders
Expand Down Expand Up @@ -420,7 +421,10 @@ imageWith :: Attr -- ^ Attributes
imageWith attr url title x = singleton $ Image attr (toList x) (url, title)

note :: Blocks -> Inlines
note = singleton . Note . toList
note = singleton . Note Footnote . toList

endNote :: Blocks -> Inlines
endNote = singleton . Note Endnote . toList

spanWith :: Attr -> Inlines -> Inlines
spanWith attr = singleton . Span attr . toList
Expand Down
27 changes: 24 additions & 3 deletions Text/Pandoc/Definition.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ module Text.Pandoc.Definition ( Pandoc(..)
, MathType(..)
, Citation(..)
, CitationMode(..)
, NoteType(..)
, pandocTypesVersion
) where

Expand Down Expand Up @@ -249,6 +250,9 @@ type Target = (String, String)
-- | Type of math element (display or inline).
data MathType = DisplayMath | InlineMath deriving (Show, Eq, Ord, Read, Typeable, Data, Generic)

-- | Type of note (footnote or endnote).
data NoteType = Footnote | Endnote deriving (Show, Eq, Ord, Read, Typeable, Data, Generic)

-- | Inline elements.
data Inline
= Str String -- ^ Text (string)
Expand All @@ -268,7 +272,7 @@ data Inline
| RawInline Format String -- ^ Raw inline
| Link Attr [Inline] Target -- ^ Hyperlink: alt text (list of inlines), target
| Image Attr [Inline] Target -- ^ Image: alt text (list of inlines), target
| Note [Block] -- ^ Footnote or endnote
| Note NoteType [Block] -- ^ Footnote or endnote
| Span Attr [Inline] -- ^ Generic inline container with attributes
deriving (Show, Eq, Ord, Read, Typeable, Data, Generic)

Expand Down Expand Up @@ -394,6 +398,20 @@ instance ToJSON MathType where
DisplayMath -> "DisplayMath"
InlineMath -> "InlineMath"

instance FromJSON NoteType where
parseJSON (Object v) = do
t <- v .: "t" :: Aeson.Parser Value
case t of
"Footnote" -> return Footnote
"Endnote" -> return Endnote
_ -> mempty
parseJSON _ = mempty
instance ToJSON NoteType where
toJSON mtype = taggedNoContent s
where s = case mtype of
Footnote -> "Footnote"
Endnote -> "Endnote"

instance FromJSON ListNumberStyle where
parseJSON (Object v) = do
t <- v .: "t" :: Aeson.Parser Value
Expand Down Expand Up @@ -483,7 +501,8 @@ instance FromJSON Inline where
return $ Link attr ils tgt
"Image" -> do (attr, ils, tgt) <- v .: "c"
return $ Image attr ils tgt
"Note" -> Note <$> v .: "c"
"Note" -> do (ntype, blks) <- v .: "c"
return $ Note ntype blks
"Span" -> do (attr, ils) <- v .: "c"
return $ Span attr ils
_ -> mempty
Expand All @@ -507,7 +526,7 @@ instance ToJSON Inline where
toJSON (RawInline fmt s) = tagged "RawInline" (fmt, s)
toJSON (Link attr ils target) = tagged "Link" (attr, ils, target)
toJSON (Image attr ils target) = tagged "Image" (attr, ils, target)
toJSON (Note blks) = tagged "Note" blks
toJSON (Note ntype blks) = tagged "Note" (ntype, blks)
toJSON (Span attr ils) = tagged "Span" (attr, ils)

instance FromJSON Block where
Expand Down Expand Up @@ -593,6 +612,7 @@ instance NFData ListNumberDelim
instance NFData ListNumberStyle
instance NFData Block
instance NFData Pandoc
instance NFData NoteType
#else
instance NFData MetaValue where rnf = genericRnf
instance NFData Meta where rnf = genericRnf
Expand All @@ -607,6 +627,7 @@ instance NFData ListNumberDelim where rnf = genericRnf
instance NFData ListNumberStyle where rnf = genericRnf
instance NFData Block where rnf = genericRnf
instance NFData Pandoc where rnf = genericRnf
instance NFData NoteType where rnf = genericRnf
#endif

pandocTypesVersion :: Version
Expand Down
4 changes: 2 additions & 2 deletions Text/Pandoc/Walk.hs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ walkInlineM f (SmallCaps xs) = SmallCaps <$> walkM f xs
walkInlineM f (Quoted qt xs) = Quoted qt <$> walkM f xs
walkInlineM f (Link atr xs t) = Link atr <$> walkM f xs <*> pure t
walkInlineM f (Image atr xs t) = Image atr <$> walkM f xs <*> pure t
walkInlineM f (Note bs) = Note <$> walkM f bs
walkInlineM f (Note t bs) = Note t <$> walkM f bs
walkInlineM f (Span attr xs) = Span attr <$> walkM f xs
walkInlineM f (Cite cs xs) = Cite <$> walkM f cs <*> walkM f xs
walkInlineM _ LineBreak = return LineBreak
Expand Down Expand Up @@ -320,7 +320,7 @@ queryInline _ (Math _ _) = mempty
queryInline _ (RawInline _ _) = mempty
queryInline f (Link _ xs _) = query f xs
queryInline f (Image _ xs _) = query f xs
queryInline f (Note bs) = query f bs
queryInline f (Note _ bs) = query f bs
queryInline f (Span _ xs) = query f xs

queryBlock :: (Walkable a Citation, Walkable a [Block],
Expand Down
14 changes: 12 additions & 2 deletions test/test-pandoc-types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,15 @@ t_image = ( Image ("id",["kls"],[("k1", "v1"), ("k2", "v2")])
, [s|{"t":"Image","c":[["id",["kls"],[["k1","v1"],["k2","v2"]]],[{"t":"Str","c":"a"},{"t":"Space"},{"t":"Str","c":"famous"},{"t":"Space"},{"t":"Str","c":"image"}],["my_img.png","image"]]}|]
)

t_footnote :: (NoteType, ByteString)
t_footnote = ( Footnote, [s|{"t":"Footnote"}|] )

t_endnote :: (NoteType, ByteString)
t_endnote = ( Endnote, [s|{"t":"Endnote"}|] )

t_note :: (Inline, ByteString)
t_note = ( Note [Para [Str "Hello"]]
, [s|{"t":"Note","c":[{"t":"Para","c":[{"t":"Str","c":"Hello"}]}]}|]
t_note = ( Note Footnote [Para [Str "Hello"]]
, [s|{"t":"Note","c":[{"t":"Footnote"},[{"t":"Para","c":[{"t":"Str","c":"Hello"}]}]]}|]
)

t_span :: (Inline, ByteString)
Expand Down Expand Up @@ -422,6 +428,10 @@ tests =
[ testEncodeDecode "DisplayMath" t_displaymath
, testEncodeDecode "InlineMath" t_inlinemath
]
, testGroup "NoteType"
[ testEncodeDecode "Footnote" t_footnote
, testEncodeDecode "Endnote" t_endnote
]
, testGroup "Inline"
[ testEncodeDecode "Str" t_str
, testEncodeDecode "Emph" t_emph
Expand Down