Skip to content

Commit 6fd38e3

Browse files
committed
Support migrations between V1.20 and V1.21
1 parent 1baf7b2 commit 6fd38e3

File tree

9 files changed

+499
-804
lines changed

9 files changed

+499
-804
lines changed

ast-migrator.cabal

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,20 @@ common common-options
4747
library
4848
import: common-options
4949
hs-source-dirs: src
50-
exposed-modules: Text.Pandoc.AST.V1_20
50+
exposed-modules: Text.Pandoc.AST.Migrator
51+
, Text.Pandoc.AST.V1_20
5152
, Text.Pandoc.AST.V1_20.Definition
53+
, Text.Pandoc.AST.V1_20.Up
5254
, Text.Pandoc.AST.V1_21
55+
, Text.Pandoc.AST.V1_21.Builder
56+
, Text.Pandoc.AST.V1_21.Definition
57+
, Text.Pandoc.AST.V1_21.Down
5358
, AstMigrator
54-
build-depends: aeson >= 0.6.2 && < 1.6
55-
, containers >= 0.3
56-
, deepseq >= 1.4.1 && < 1.5
57-
, syb >= 0.1 && < 0.8
59+
build-depends: aeson >= 0.6.2 && < 1.6
60+
, containers >= 0.3
61+
, deepseq >= 1.4.1 && < 1.5
62+
, pandoc-types == 1.21.*
63+
, syb >= 0.1 && < 0.8
5864
, text
5965

6066
executable ast-migrator

src/Text/Pandoc/AST/Migrator.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{- |
2+
Module : Text.Pandoc.AST.Migrator
3+
Copyright : © 2020 Albert Krewinkel
4+
License : MIT
5+
6+
Maintainer : Albert Krewinkel <albert@zeitkraut.de>
7+
Stability : alpha
8+
Portability : portable
9+
10+
Migrate from or to older AST versions.
11+
-}
12+
module Text.Pandoc.AST.Migrator
13+
( migrateUpToV1_21
14+
, migrateDownFromV1_21
15+
) where
16+
17+
import Text.Pandoc.AST.V1_20.Up (migrateUpToV1_21)
18+
import Text.Pandoc.AST.V1_21.Down (migrateDownFromV1_21)

src/Text/Pandoc/AST/V1_20.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Version 1.20 of the pandoc document AST.
1111
-}
1212
module Text.Pandoc.AST.V1_20
1313
( module Text.Pandoc.AST.V1_20.Definition
14+
, migrateUpToV1_21
1415
) where
1516

1617
import Text.Pandoc.AST.V1_20.Definition
18+
import Text.Pandoc.AST.V1_20.Up (migrateUpToV1_21)

src/Text/Pandoc/AST/V1_20/Up.hs

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
{-# LANGUAGE LambdaCase #-}
2+
{-# LANGUAGE OverloadedStrings #-}
3+
{- |
4+
Module : Text.Pandoc.AST.V1_20.Up
5+
Copyright : © 2020 Albert Krewinkel
6+
License : MIT
7+
8+
Maintainer : Albert Krewinkel <albert@zeitkraut.de>
9+
Stability : alpha
10+
Portability : portable
11+
12+
Version 1.20 of the pandoc document AST.
13+
-}
14+
module Text.Pandoc.AST.V1_20.Up
15+
( migrateUpToV1_21
16+
, migrateUp
17+
-- , migrateDownToV1_20
18+
) where
19+
20+
import Text.Pandoc.AST.V1_20.Definition
21+
import qualified Text.Pandoc.AST.V1_21.Definition as V1_21
22+
import qualified Data.Map as M
23+
24+
migrateUp :: Pandoc -> V1_21.Pandoc
25+
migrateUp = migrateUpToV1_21
26+
27+
migrateUpToV1_21 :: Pandoc -> V1_21.Pandoc
28+
migrateUpToV1_21 (Pandoc meta blocks) =
29+
V1_21.Pandoc (migrateMeta meta) (map migrateBlock blocks)
30+
31+
migrateMeta :: Meta -> V1_21.Meta
32+
migrateMeta = V1_21.Meta . M.map migrateMetaValue . unMeta
33+
34+
migrateBlock :: Block -> V1_21.Block
35+
migrateBlock = \case
36+
BlockQuote blks -> V1_21.BlockQuote $ migrateBlocks blks
37+
BulletList items -> V1_21.BulletList $ migrateItems items
38+
CodeBlock attr text -> V1_21.CodeBlock attr text
39+
DefinitionList defItems -> V1_21.DefinitionList
40+
$ map migrateDefItem defItems
41+
Div attr blks -> V1_21.Div attr $ migrateBlocks blks
42+
Header lvl attr inlns -> V1_21.Header lvl attr
43+
$ migrateInlines inlns
44+
HorizontalRule -> V1_21.HorizontalRule
45+
LineBlock lines' -> V1_21.LineBlock $ map migrateInlines lines'
46+
Null -> V1_21.Null
47+
OrderedList listAttr items -> V1_21.OrderedList
48+
(migrateListAttributes listAttr)
49+
(migrateItems items)
50+
Para inlns -> V1_21.Para $ migrateInlines inlns
51+
Plain inlns -> V1_21.Plain $ migrateInlines inlns
52+
RawBlock f text -> V1_21.RawBlock (migrateFormat f) text
53+
Table capt as ws h rows -> migrateTable capt as ws h rows
54+
where
55+
migrateBlocks = map migrateBlock
56+
migrateInlines = map migrateInline
57+
migrateItems = map migrateBlocks
58+
migrateDefItem (def, items) = (migrateInlines def, migrateItems items)
59+
migrateTable caption aligns widths head' body' =
60+
V1_21.Table nullAttr
61+
(V1_21.Caption Nothing [V1_21.Plain (migrateInlines caption)])
62+
(zip (map migrateAlignment aligns) (map toColWidth widths))
63+
(V1_21.TableHead nullAttr [migrateRow head'])
64+
[V1_21.TableBody nullAttr
65+
(V1_21.RowHeadColumns 0)
66+
[]
67+
(map migrateRow body')]
68+
(V1_21.TableFoot nullAttr [])
69+
migrateRow cells = V1_21.Row nullAttr (map migrateCell cells)
70+
migrateCell blks = V1_21.Cell nullAttr V1_21.AlignDefault
71+
(V1_21.RowSpan 1)
72+
(V1_21.ColSpan 1)
73+
(migrateBlocks blks)
74+
toColWidth = \case
75+
0 -> V1_21.ColWidthDefault
76+
d -> V1_21.ColWidth d
77+
78+
migrateMetaValue :: MetaValue -> V1_21.MetaValue
79+
migrateMetaValue = \case
80+
MetaBlocks blocks -> V1_21.MetaBlocks $ map migrateBlock blocks
81+
MetaBool b -> V1_21.MetaBool b
82+
MetaInlines inlines -> V1_21.MetaInlines $ map migrateInline inlines
83+
MetaList vs -> V1_21.MetaList $ map migrateMetaValue vs
84+
MetaMap metamap -> V1_21.MetaMap $ M.map migrateMetaValue metamap
85+
MetaString s -> V1_21.MetaString s
86+
87+
migrateAlignment :: Alignment -> V1_21.Alignment
88+
migrateAlignment = \case
89+
AlignLeft -> V1_21.AlignLeft
90+
AlignRight -> V1_21.AlignRight
91+
AlignCenter -> V1_21.AlignCenter
92+
AlignDefault -> V1_21.AlignDefault
93+
94+
migrateListNumberDelim :: ListNumberDelim -> V1_21.ListNumberDelim
95+
migrateListNumberDelim = \case
96+
DefaultDelim -> V1_21.DefaultDelim
97+
Period -> V1_21.Period
98+
OneParen -> V1_21.OneParen
99+
TwoParens -> V1_21.TwoParens
100+
101+
migrateListNumberStyle :: ListNumberStyle -> V1_21.ListNumberStyle
102+
migrateListNumberStyle = \case
103+
DefaultStyle -> V1_21.DefaultStyle
104+
Example -> V1_21.Example
105+
Decimal -> V1_21.Decimal
106+
LowerRoman -> V1_21.LowerRoman
107+
UpperRoman -> V1_21.UpperRoman
108+
LowerAlpha -> V1_21.LowerAlpha
109+
UpperAlpha -> V1_21.UpperAlpha
110+
111+
migrateListAttributes :: ListAttributes -> V1_21.ListAttributes
112+
migrateListAttributes (n, style, delim) =
113+
(n, migrateListNumberStyle style, migrateListNumberDelim delim)
114+
115+
migrateFormat :: Format -> V1_21.Format
116+
migrateFormat (Format f) = V1_21.Format f
117+
118+
migrateQuoteType :: QuoteType -> V1_21.QuoteType
119+
migrateQuoteType = \case
120+
SingleQuote -> V1_21.SingleQuote
121+
DoubleQuote -> V1_21.DoubleQuote
122+
123+
migrateMathType :: MathType -> V1_21.MathType
124+
migrateMathType = \case
125+
DisplayMath -> V1_21.DisplayMath
126+
InlineMath -> V1_21.InlineMath
127+
128+
migrateInline :: Inline -> V1_21.Inline
129+
migrateInline = \case
130+
Cite citations inlns -> V1_21.Cite (migrateCitations citations) $ migrateInlines inlns
131+
Code attr text -> V1_21.Code attr text
132+
Emph inlns -> V1_21.Emph $ migrateInlines inlns
133+
Image attr inlns tgt -> V1_21.Image attr (migrateInlines inlns) tgt
134+
LineBreak -> V1_21.LineBreak
135+
Link attr inlns tgt -> V1_21.Link attr (migrateInlines inlns) tgt
136+
Math mathType text -> V1_21.Math (migrateMathType mathType) text
137+
Note blks -> V1_21.Note $ migrateBlocks blks
138+
Quoted qtype inlns -> V1_21.Quoted (migrateQuoteType qtype) $ migrateInlines inlns
139+
RawInline f text -> V1_21.RawInline (migrateFormat f) text
140+
SmallCaps inlns -> V1_21.SmallCaps $ migrateInlines inlns
141+
SoftBreak -> V1_21.SoftBreak
142+
Space -> V1_21.Space
143+
Span attr inlns -> V1_21.Span attr $ migrateInlines inlns
144+
Str text -> V1_21.Str text
145+
Strikeout inlns -> V1_21.Strikeout $ migrateInlines inlns
146+
Strong inlns -> V1_21.Strong $ migrateInlines inlns
147+
Subscript inlns -> V1_21.Subscript $ migrateInlines inlns
148+
Superscript inlns -> V1_21.Superscript $ migrateInlines inlns
149+
where
150+
migrateBlocks = map migrateBlock
151+
migrateInlines = map migrateInline
152+
migrateCitations = map migrateCitation
153+
154+
migrateCitation :: Citation -> V1_21.Citation
155+
migrateCitation citation = V1_21.Citation
156+
(citationId citation)
157+
(map migrateInline $ citationPrefix citation)
158+
(map migrateInline $ citationSuffix citation)
159+
(migrateCitationMode $ citationMode citation)
160+
(citationNoteNum citation)
161+
(citationHash citation)
162+
163+
migrateCitationMode :: CitationMode -> V1_21.CitationMode
164+
migrateCitationMode = \case
165+
AuthorInText -> V1_21.AuthorInText
166+
NormalCitation -> V1_21.NormalCitation
167+
SuppressAuthor -> V1_21.SuppressAuthor

0 commit comments

Comments
 (0)