gdritter repos ptolemy / 04be08c
Ptolemy-bridge project for converting back and forth between Pandoc types and Ptolemy types Getty Ritter 8 years ago
3 changed file(s) with 217 addition(s) and 1 deletion(s). Collapse all Expand all
1 projects: ptolemy-core/ptolemy-core.cabal,
1 packages: ptolemy-core/ptolemy-core.cabal,
22 ptolemy-reader-markdown/ptolemy-reader-markdown.cabal,
33 ptolemy-writer-html/ptolemy-writer-html.cabal,
4 ptolemy-bridge/ptolemy-bridge.cabal
45 ptolemy/ptolemy.cabal
1 {-# LANGUAGE FunctionalDependencies #-}
2 {-# LANGUAGE TypeSynonymInstances #-}
3 {-# LANGUAGE FlexibleInstances #-}
4
5 module Text.Ptolemy.Pandoc where
6
7 import Data.Vector (Vector)
8 import qualified Data.Vector as V
9 import Data.Text (Text)
10 import qualified Data.Text as T
11 import Text.Ptolemy.Core
12 import qualified Text.Pandoc.Definition as P
13
14 -- This module contains the entirely mechanical translation between the
15 -- Pandoc types and their corresponding Ptolemy types.
16
17 class Convert pan pto where
18 from :: pan -> pto
19 to :: pto -> pan
20
21 instance Convert x y => Convert [x] (Vector y) where
22 from xs = V.fromList (map from xs)
23 to ys = map to (V.toList ys)
24
25 instance Convert String Text where
26 from = T.pack
27 to = T.unpack
28
29 instance (Convert a b, Convert c d) => Convert (a, c) (b, d) where
30 from (a, b) = (from a, from b)
31 to (a, b) = (to a, to b)
32
33 instance Convert P.Block Block where
34 from (P.Plain is) = Plain (from is)
35 from (P.Para is) = Para (from is)
36 from (P.CodeBlock as str) = CodeBlock (from as) (from str)
37 from (P.RawBlock fmt str) = RawBlock (from fmt) (from str)
38 from (P.BlockQuote bs) = BlockQuote (from bs)
39 from (P.OrderedList la bs) = OrderedList (from la) (from bs)
40 from (P.BulletList bs) = BulletList (from bs)
41 from (P.DefinitionList ds) = DefinitionList (from ds)
42 from (P.Header n attr is) = Header n (from attr) (from is)
43 from (P.HorizontalRule) = HorizontalRule
44 from (P.Div attr bs) = Div (from attr) (from bs)
45 from (P.Null) = Null
46 to (Plain is) = P.Plain (to is)
47 to (Para is) = P.Para (to is)
48 to (CodeBlock as str) = P.CodeBlock (to as) (to str)
49 to (RawBlock fmt str) = P.RawBlock (to fmt) (to str)
50 to (BlockQuote bs) = P.BlockQuote (to bs)
51 to (OrderedList la bs) = P.OrderedList (to la) (to bs)
52 to (BulletList bs) = P.BulletList (to bs)
53 to (DefinitionList ds) = P.DefinitionList (to ds)
54 to (Header n attr is) = P.Header n (to attr) (to is)
55 to (HorizontalRule) = P.HorizontalRule
56 to (Div attr bs) = P.Div (to attr) (to bs)
57 to (Null) = P.Null
58
59 instance Convert P.Inline Inline where
60 from (P.Str str) = Str (from str)
61 from (P.Emph is) = Emph (from is)
62 from (P.Strong is) = Strong (from is)
63 from (P.Strikeout is) = Strikeout (from is)
64 from (P.Superscript is) = Superscript (from is)
65 from (P.Subscript is) = Subscript (from is)
66 from (P.SmallCaps is) = SmallCaps (from is)
67 from (P.Quoted qt is) = Quoted (from qt) (from is)
68 from (P.Cite bs is) = Cite (from bs) (from is)
69 from (P.Code attr str) = Code (from attr) (from str)
70 from (P.Space) = Space
71 from (P.SoftBreak) = SoftBreak
72 from (P.LineBreak) = LineBreak
73 from (P.Math mt str) = Math (from mt) (from str)
74 from (P.RawInline fmt str) = RawInline (from fmt) (from str)
75 from (P.Link attr is tgt) = Link (from attr) (from is) (from tgt)
76 from (P.Image attr is tgt) = Image (from attr) (from is) (from tgt)
77 from (P.Note bs) = Note (from bs)
78 from (P.Span attr is) = Span (from attr) (from is)
79 to (Str str) = P.Str (to str)
80 to (Emph is) = P.Emph (to is)
81 to (Strong is) = P.Strong (to is)
82 to (Strikeout is) = P.Strikeout (to is)
83 to (Superscript is) = P.Superscript (to is)
84 to (Subscript is) = P.Subscript (to is)
85 to (SmallCaps is) = P.SmallCaps (to is)
86 to (Quoted qt is) = P.Quoted (to qt) (to is)
87 to (Cite bs is) = P.Cite (to bs) (to is)
88 to (Code attr str) = P.Code (to attr) (to str)
89 to (Space) = P.Space
90 to (SoftBreak) = P.SoftBreak
91 to (LineBreak) = P.LineBreak
92 to (Math mt str) = P.Math (to mt) (to str)
93 to (RawInline fmt str) = P.RawInline (to fmt) (to str)
94 to (Link attr is tgt) = P.Link (to attr) (to is) (to tgt)
95 to (Image attr is tgt) = P.Image (to attr) (to is) (to tgt)
96 to (Note bs) = P.Note (to bs)
97 to (Span attr is) = P.Span (to attr) (to is)
98
99 instance Convert P.Citation Citation where
100 from cite = Citation
101 { ciId = from (P.citationId cite)
102 , ciPrefix = from (P.citationPrefix cite)
103 , ciSuffix = from (P.citationSuffix cite)
104 , ciMode = from (P.citationMode cite)
105 , ciNoteNum = P.citationNoteNum cite
106 , ciHash = P.citationHash cite
107 }
108 to cite = P.Citation
109 { P.citationId = to (ciId cite)
110 , P.citationPrefix = to (ciPrefix cite)
111 , P.citationSuffix = to (ciSuffix cite)
112 , P.citationMode = to (ciMode cite)
113 , P.citationNoteNum = ciNoteNum cite
114 , P.citationHash = ciHash cite
115 }
116
117 instance Convert P.CitationMode CitationMode where
118 from P.AuthorInText = AuthorInText
119 from P.SuppressAuthor = SuppressAuthor
120 from P.NormalCitation = NormalCitation
121 to AuthorInText = P.AuthorInText
122 to SuppressAuthor = P.SuppressAuthor
123 to NormalCitation = P.NormalCitation
124
125 instance Convert P.MathType MathType where
126 from P.DisplayMath = DisplayMath
127 from P.InlineMath = InlineMath
128 to DisplayMath = P.DisplayMath
129 to InlineMath = P.InlineMath
130
131 instance Convert P.Format Format where
132 from (P.Format str) = Format (from str)
133 to (Format str) = P.Format (to str)
134
135 instance Convert P.Attr Attr where
136 from (ident, classes, props) = Attr
137 { attrIdentifier = from ident
138 , attrClasses = from classes
139 , attrProps = from props
140 }
141 to attr = ( to (attrIdentifier attr)
142 , to (attrClasses attr)
143 , to (attrProps attr)
144 )
145
146 instance Convert P.ListAttributes ListAttributes where
147 from (i, lsy, ldl) = ListAttributes
148 { laWhatever = i
149 , laNumberStyle = from lsy
150 , laNumberDelim = from ldl
151 }
152 to lattrs = ( laWhatever lattrs
153 , to (laNumberStyle lattrs)
154 , to (laNumberDelim lattrs)
155 )
156
157 instance Convert P.ListNumberStyle ListNumberStyle where
158 from P.DefaultStyle = DefaultStyle
159 from P.Example = Example
160 from P.Decimal = Decimal
161 from P.LowerRoman = LowerRoman
162 from P.UpperRoman = UpperRoman
163 from P.LowerAlpha = LowerAlpha
164 from P.UpperAlpha = UpperAlpha
165 to DefaultStyle = P.DefaultStyle
166 to Example = P.Example
167 to Decimal = P.Decimal
168 to LowerRoman = P.LowerRoman
169 to UpperRoman = P.UpperRoman
170 to LowerAlpha = P.LowerAlpha
171 to UpperAlpha = P.UpperAlpha
172
173 instance Convert P.ListNumberDelim ListNumberDelim where
174 from P.DefaultDelim = DefaultDelim
175 from P.Period = Period
176 from P.OneParen = OneParen
177 from P.TwoParens = TwoParens
178 to DefaultDelim = P.DefaultDelim
179 to Period = P.Period
180 to OneParen = P.OneParen
181 to TwoParens = P.TwoParens
182
183 instance Convert P.QuoteType QuoteType where
184 from P.SingleQuote = SingleQuote
185 from P.DoubleQuote = DoubleQuote
186 to SingleQuote = P.SingleQuote
187 to DoubleQuote = P.DoubleQuote
188
189 instance Convert P.Target Target where
190 from (url, title) = Target { tgtURL = from url, tgtTitle = from title }
191 to tgt = (to (tgtURL tgt), to (tgtTitle tgt))
1 name: ptolemy-bridge
2 version: 0.1.0.0
3 -- synopsis:
4 -- description
5 license: BSD3
6 license-file: LICENSE
7 author: Getty Ritter
8 maintainer: gettyritter@gmail.com
9 copyright: ©2016 Getty Ritter
10 category: Text
11 build-type: Simple
12 cabal-version: >= 1.2
13
14 library
15 exposed-modules: Text.Ptolemy.Pandoc
16 ghc-options: -Wall
17 build-depends: base >=4.7 && <4.9,
18 ptolemy-core,
19 text,
20 vector,
21 pandoc-types
22 default-language: Haskell2010
23 default-extensions: OverloadedStrings,
24 ScopedTypeVariables