Pandoc.hs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. {-# LANGUAGE FunctionalDependencies #-}
  2. {-# LANGUAGE TypeSynonymInstances #-}
  3. {-# LANGUAGE FlexibleInstances #-}
  4. module Text.Ptolemy.Pandoc where
  5. import Data.Vector (Vector)
  6. import qualified Data.Vector as V
  7. import Data.Text (Text)
  8. import qualified Data.Text as T
  9. import Text.Ptolemy.Core
  10. import qualified Text.Pandoc.Definition as P
  11. -- This module contains the entirely mechanical translation between the
  12. -- Pandoc types and their corresponding Ptolemy types.
  13. class Convert pan pto where
  14. from :: pan -> pto
  15. to :: pto -> pan
  16. instance Convert x y => Convert [x] (Vector y) where
  17. from xs = V.fromList (map from xs)
  18. to ys = map to (V.toList ys)
  19. instance Convert String Text where
  20. from = T.pack
  21. to = T.unpack
  22. instance (Convert a b, Convert c d) => Convert (a, c) (b, d) where
  23. from (a, b) = (from a, from b)
  24. to (a, b) = (to a, to b)
  25. instance Convert P.Block Block where
  26. from (P.Plain is) = Plain (from is)
  27. from (P.Para is) = Para (from is)
  28. from (P.CodeBlock as str) = CodeBlock (from as) (from str)
  29. from (P.RawBlock fmt str) = RawBlock (from fmt) (from str)
  30. from (P.BlockQuote bs) = BlockQuote (from bs)
  31. from (P.OrderedList la bs) = OrderedList (from la) (from bs)
  32. from (P.BulletList bs) = BulletList (from bs)
  33. from (P.DefinitionList ds) = DefinitionList (from ds)
  34. from (P.Header n attr is) = Header n (from attr) (from is)
  35. from (P.HorizontalRule) = HorizontalRule
  36. from (P.Div attr bs) = Div (from attr) (from bs)
  37. from (P.Null) = Null
  38. to (Plain is) = P.Plain (to is)
  39. to (Para is) = P.Para (to is)
  40. to (CodeBlock as str) = P.CodeBlock (to as) (to str)
  41. to (RawBlock fmt str) = P.RawBlock (to fmt) (to str)
  42. to (BlockQuote bs) = P.BlockQuote (to bs)
  43. to (OrderedList la bs) = P.OrderedList (to la) (to bs)
  44. to (BulletList bs) = P.BulletList (to bs)
  45. to (DefinitionList ds) = P.DefinitionList (to ds)
  46. to (Header n attr is) = P.Header n (to attr) (to is)
  47. to (HorizontalRule) = P.HorizontalRule
  48. to (Div attr bs) = P.Div (to attr) (to bs)
  49. to (Null) = P.Null
  50. instance Convert P.Inline Inline where
  51. from (P.Str str) = Str (from str)
  52. from (P.Emph is) = Emph (from is)
  53. from (P.Strong is) = Strong (from is)
  54. from (P.Strikeout is) = Strikeout (from is)
  55. from (P.Superscript is) = Superscript (from is)
  56. from (P.Subscript is) = Subscript (from is)
  57. from (P.SmallCaps is) = SmallCaps (from is)
  58. from (P.Quoted qt is) = Quoted (from qt) (from is)
  59. from (P.Cite bs is) = Cite (from bs) (from is)
  60. from (P.Code attr str) = Code (from attr) (from str)
  61. from (P.Space) = Space
  62. from (P.SoftBreak) = SoftBreak
  63. from (P.LineBreak) = LineBreak
  64. from (P.Math mt str) = Math (from mt) (from str)
  65. from (P.RawInline fmt str) = RawInline (from fmt) (from str)
  66. from (P.Link attr is tgt) = Link (from attr) (from is) (from tgt)
  67. from (P.Image attr is tgt) = Image (from attr) (from is) (from tgt)
  68. from (P.Note bs) = Note (from bs)
  69. from (P.Span attr is) = Span (from attr) (from is)
  70. to (Str str) = P.Str (to str)
  71. to (Emph is) = P.Emph (to is)
  72. to (Strong is) = P.Strong (to is)
  73. to (Strikeout is) = P.Strikeout (to is)
  74. to (Superscript is) = P.Superscript (to is)
  75. to (Subscript is) = P.Subscript (to is)
  76. to (SmallCaps is) = P.SmallCaps (to is)
  77. to (Quoted qt is) = P.Quoted (to qt) (to is)
  78. to (Cite bs is) = P.Cite (to bs) (to is)
  79. to (Code attr str) = P.Code (to attr) (to str)
  80. to (Space) = P.Space
  81. to (SoftBreak) = P.SoftBreak
  82. to (LineBreak) = P.LineBreak
  83. to (Math mt str) = P.Math (to mt) (to str)
  84. to (RawInline fmt str) = P.RawInline (to fmt) (to str)
  85. to (Link attr is tgt) = P.Link (to attr) (to is) (to tgt)
  86. to (Image attr is tgt) = P.Image (to attr) (to is) (to tgt)
  87. to (Note bs) = P.Note (to bs)
  88. to (Span attr is) = P.Span (to attr) (to is)
  89. instance Convert P.Citation Citation where
  90. from cite = Citation
  91. { ciId = from (P.citationId cite)
  92. , ciPrefix = from (P.citationPrefix cite)
  93. , ciSuffix = from (P.citationSuffix cite)
  94. , ciMode = from (P.citationMode cite)
  95. , ciNoteNum = P.citationNoteNum cite
  96. , ciHash = P.citationHash cite
  97. }
  98. to cite = P.Citation
  99. { P.citationId = to (ciId cite)
  100. , P.citationPrefix = to (ciPrefix cite)
  101. , P.citationSuffix = to (ciSuffix cite)
  102. , P.citationMode = to (ciMode cite)
  103. , P.citationNoteNum = ciNoteNum cite
  104. , P.citationHash = ciHash cite
  105. }
  106. instance Convert P.CitationMode CitationMode where
  107. from P.AuthorInText = AuthorInText
  108. from P.SuppressAuthor = SuppressAuthor
  109. from P.NormalCitation = NormalCitation
  110. to AuthorInText = P.AuthorInText
  111. to SuppressAuthor = P.SuppressAuthor
  112. to NormalCitation = P.NormalCitation
  113. instance Convert P.MathType MathType where
  114. from P.DisplayMath = DisplayMath
  115. from P.InlineMath = InlineMath
  116. to DisplayMath = P.DisplayMath
  117. to InlineMath = P.InlineMath
  118. instance Convert P.Format Format where
  119. from (P.Format str) = Format (from str)
  120. to (Format str) = P.Format (to str)
  121. instance Convert P.Attr Attr where
  122. from (ident, classes, props) = Attr
  123. { attrIdentifier = from ident
  124. , attrClasses = from classes
  125. , attrProps = from props
  126. }
  127. to attr = ( to (attrIdentifier attr)
  128. , to (attrClasses attr)
  129. , to (attrProps attr)
  130. )
  131. instance Convert P.ListAttributes ListAttributes where
  132. from (i, lsy, ldl) = ListAttributes
  133. { laWhatever = i
  134. , laNumberStyle = from lsy
  135. , laNumberDelim = from ldl
  136. }
  137. to lattrs = ( laWhatever lattrs
  138. , to (laNumberStyle lattrs)
  139. , to (laNumberDelim lattrs)
  140. )
  141. instance Convert P.ListNumberStyle ListNumberStyle where
  142. from P.DefaultStyle = DefaultStyle
  143. from P.Example = Example
  144. from P.Decimal = Decimal
  145. from P.LowerRoman = LowerRoman
  146. from P.UpperRoman = UpperRoman
  147. from P.LowerAlpha = LowerAlpha
  148. from P.UpperAlpha = UpperAlpha
  149. to DefaultStyle = P.DefaultStyle
  150. to Example = P.Example
  151. to Decimal = P.Decimal
  152. to LowerRoman = P.LowerRoman
  153. to UpperRoman = P.UpperRoman
  154. to LowerAlpha = P.LowerAlpha
  155. to UpperAlpha = P.UpperAlpha
  156. instance Convert P.ListNumberDelim ListNumberDelim where
  157. from P.DefaultDelim = DefaultDelim
  158. from P.Period = Period
  159. from P.OneParen = OneParen
  160. from P.TwoParens = TwoParens
  161. to DefaultDelim = P.DefaultDelim
  162. to Period = P.Period
  163. to OneParen = P.OneParen
  164. to TwoParens = P.TwoParens
  165. instance Convert P.QuoteType QuoteType where
  166. from P.SingleQuote = SingleQuote
  167. from P.DoubleQuote = DoubleQuote
  168. to SingleQuote = P.SingleQuote
  169. to DoubleQuote = P.DoubleQuote
  170. instance Convert P.Target Target where
  171. from (url, title) = Target { tgtURL = from url, tgtTitle = from title }
  172. to tgt = (to (tgtURL tgt), to (tgtTitle tgt))