]> git.pld-linux.org Git - packages/pandoc.git/blob - jira-wiki-markup-1.3.patch
rediff patch
[packages/pandoc.git] / jira-wiki-markup-1.3.patch
1 From 69a3fa57087f54df6fe24c447cfbad8563befd36 Mon Sep 17 00:00:00 2001
2 From: Albert Krewinkel <albert@zeitkraut.de>
3 Date: Mon, 30 Mar 2020 22:03:52 +0200
4 Subject: [PATCH] Jira reader: retain image attributes
5
6 Jira images attributes as in `!image.jpg|align=right!` are retained as
7 key-value pairs. Thumbnail images, such as `!example.gif|thumbnail!`,
8 are marked by a `thumbnail` class in their attributes.
9
10 Related to #6234.
11 ---
12  src/Text/Pandoc/Readers/Jira.hs | 14 +++++++++++++-
13  test/Tests/Readers/Jira.hs      |  9 +++++++++
14  2 files changed, 22 insertions(+), 1 deletion(-)
15
16 diff --git a/src/Text/Pandoc/Readers/Jira.hs b/src/Text/Pandoc/Readers/Jira.hs
17 index 117239d409..cfe8ba81c1 100644
18 --- a/src/Text/Pandoc/Readers/Jira.hs
19 +++ b/src/Text/Pandoc/Readers/Jira.hs
20 @@ -123,7 +123,8 @@ jiraToPandocInlines = \case
21                                       fromInlines ils
22    Jira.Emoji icon        -> str . iconUnicode $ icon
23    Jira.Entity entity     -> str . fromEntity $ entity
24 -  Jira.Image _ url       -> image (Jira.fromURL url)  "" mempty
25 +  Jira.Image params url  -> let (title, attr) = imgParams params
26 +                            in imageWith attr (Jira.fromURL url) title mempty
27    Jira.Link alias url    -> link (Jira.fromURL url) "" (fromInlines alias)
28    Jira.Linebreak         -> linebreak
29    Jira.Monospaced inlns  -> code . stringify . toList . fromInlines $ inlns
30 @@ -145,6 +146,17 @@ jiraToPandocInlines = \case
31        Jira.Subscript   -> subscript
32        Jira.Superscript -> superscript
33  
34 +    imgParams :: [Jira.Parameter] -> (Text, Attr)
35 +    imgParams = foldr addImgParam ("", ("", [], []))
36 +
37 +    addImgParam :: Jira.Parameter -> (Text, Attr) -> (Text, Attr)
38 +    addImgParam p (title, attr@(ident, classes, kvs)) =
39 +      case Jira.parameterKey p of
40 +        "title"     -> (Jira.parameterValue p, attr)
41 +        "thumbnail" -> (title, (ident, "thumbnail":classes, kvs))
42 +        _           -> let kv = (Jira.parameterKey p, Jira.parameterValue p)
43 +                       in (title, (ident, classes, kv:kvs))
44 +
45  -- | Get unicode representation of a Jira icon.
46  iconUnicode :: Jira.Icon -> Text
47  iconUnicode = \case
48 diff --git a/test/Tests/Readers/Jira.hs b/test/Tests/Readers/Jira.hs
49 index 299db7bed3..1ae3244ab4 100644
50 --- a/test/Tests/Readers/Jira.hs
51 +++ b/test/Tests/Readers/Jira.hs
52 @@ -117,6 +117,15 @@ tests =
53        "!https://example.com/image.jpg!" =?>
54        para (image "https://example.com/image.jpg" "" mempty)
55  
56 +    , "thumbnail image" =:
57 +      "!image.jpg|thumbnail!" =?>
58 +      para (imageWith ("", ["thumbnail"], []) "image.jpg" "" mempty)
59 +
60 +    , "image with attributes" =:
61 +      "!image.gif|align=right, vspace=4, title=Hello!" =?>
62 +      let attr = ("", [], [("align", "right"), ("vspace", "4")])
63 +      in para $ imageWith attr "image.gif" "Hello" mempty
64 +
65      , "HTML entity" =:
66        "me &amp; you" =?> para "me & you"
67  
68 From c3f539364aea5065be1d6774cd62f40a1918e773 Mon Sep 17 00:00:00 2001
69 From: Albert Krewinkel <albert@zeitkraut.de>
70 Date: Sat, 4 Apr 2020 14:27:27 +0200
71 Subject: [PATCH] Jira: support citations, attachment links, and user links
72
73 Closes: #6231
74 Closes: #6238
75 Closes: #6239
76 ---
77  pandoc.cabal                    |  2 +-
78  src/Text/Pandoc/Readers/Jira.hs | 16 ++++++++++++++-
79  src/Text/Pandoc/Writers/Jira.hs | 28 ++++++++++++++++++++++---
80  stack.yaml                      |  2 +-
81  test/Tests/Readers/Jira.hs      | 36 ++++++++++++++++++++++++++++++---
82  test/Tests/Writers/Jira.hs      | 34 +++++++++++++++++++++++++++++++
83  6 files changed, 109 insertions(+), 9 deletions(-)
84
85 diff --git a/pandoc.cabal b/pandoc.cabal
86 index 279cce80ab..529b3368f1 100644
87 --- a/pandoc.cabal
88 +++ b/pandoc.cabal
89 @@ -412,7 +412,7 @@ library
90                   blaze-html >= 0.9 && < 0.10,
91                   blaze-markup >= 0.8 && < 0.9,
92                   vector >= 0.10 && < 0.13,
93 -                 jira-wiki-markup >= 1.1.3,
94 +                 jira-wiki-markup >= 1.3 && < 1.4,
95                   hslua >= 1.0.1,
96                   hslua-module-system >= 0.2 && < 0.3,
97                   hslua-module-text >= 0.2 && < 0.3,
98 diff --git a/src/Text/Pandoc/Readers/Jira.hs b/src/Text/Pandoc/Readers/Jira.hs
99 index d6fa688e33..46723f944a 100644
100 --- a/src/Text/Pandoc/Readers/Jira.hs
101 +++ b/src/Text/Pandoc/Readers/Jira.hs
102 @@ -119,13 +119,14 @@ jiraToPandocInlines :: Jira.Inline -> Inlines
103  jiraToPandocInlines = \case
104    Jira.Anchor t          -> spanWith (t, [], []) mempty
105    Jira.AutoLink url      -> link (Jira.fromURL url) "" (str (Jira.fromURL url))
106 +  Jira.Citation ils      -> str "—" <> space <> emph (fromInlines ils)
107    Jira.ColorInline c ils -> spanWith ("", [], [("color", colorName c)]) $
108                                       fromInlines ils
109    Jira.Emoji icon        -> str . iconUnicode $ icon
110    Jira.Entity entity     -> str . fromEntity $ entity
111    Jira.Image params url  -> let (title, attr) = imgParams params
112                              in imageWith attr (Jira.fromURL url) title mempty
113 -  Jira.Link alias url    -> link (Jira.fromURL url) "" (fromInlines alias)
114 +  Jira.Link lt alias url -> jiraLinkToPandoc lt alias url
115    Jira.Linebreak         -> linebreak
116    Jira.Monospaced inlns  -> code . stringify . toList . fromInlines $ inlns
117    Jira.Space             -> space
118 @@ -157,6 +158,19 @@ jiraToPandocInlines = \case
119          _           -> let kv = (Jira.parameterKey p, Jira.parameterValue p)
120                         in (title, (ident, classes, kv:kvs))
121  
122 +-- | Convert a Jira link to pandoc inlines.
123 +jiraLinkToPandoc :: Jira.LinkType -> [Jira.Inline] -> Jira.URL -> Inlines
124 +jiraLinkToPandoc linkType alias url =
125 +  let url' = (if linkType == Jira.User then ("~" <>) else id) $ Jira.fromURL url
126 +      alias' = case alias of
127 +                 [] -> str url'
128 +                 _  -> foldMap jiraToPandocInlines alias
129 +  in case linkType of
130 +    Jira.External   -> link url' "" alias'
131 +    Jira.Email      -> link ("mailto:" <> url') "" alias'
132 +    Jira.Attachment -> linkWith ("", ["attachment"], []) url' "" alias'
133 +    Jira.User       -> linkWith ("", ["user-account"], []) url' "" alias'
134 +
135  -- | Get unicode representation of a Jira icon.
136  iconUnicode :: Jira.Icon -> Text
137  iconUnicode = \case
138 diff --git a/src/Text/Pandoc/Writers/Jira.hs b/src/Text/Pandoc/Writers/Jira.hs
139 index d1a6566875..19db341374 100644
140 --- a/src/Text/Pandoc/Writers/Jira.hs
141 +++ b/src/Text/Pandoc/Writers/Jira.hs
142 @@ -1,5 +1,6 @@
143  {-# LANGUAGE LambdaCase #-}
144  {-# LANGUAGE OverloadedStrings #-}
145 +{-# LANGUAGE PatternGuards #-}
146  {- |
147     Module      : Text.Pandoc.Writers.Jira
148     Copyright   : © 2010-2020 Albert Krewinkel, John MacFarlane
149 @@ -25,7 +26,7 @@ import Text.Pandoc.Class.PandocMonad (PandocMonad)
150  import Text.Pandoc.Definition
151  import Text.Pandoc.Options (WriterOptions (writerTemplate, writerWrapText),
152                              WrapOption (..))
153 -import Text.Pandoc.Shared (linesToPara)
154 +import Text.Pandoc.Shared (linesToPara, stringify)
155  import Text.Pandoc.Templates (renderTemplate)
156  import Text.Pandoc.Writers.Math (texMathToInlines)
157  import Text.Pandoc.Writers.Shared (defField, metaToContext)
158 @@ -193,8 +194,7 @@ toJiraInlines inlines = do
159          Emph xs            -> styled Jira.Emphasis xs
160          Image _ _ (src, _) -> pure . singleton $ Jira.Image [] (Jira.URL src)
161          LineBreak          -> pure . singleton $ Jira.Linebreak
162 -        Link _ xs (tgt, _) -> singleton . flip Jira.Link (Jira.URL tgt)
163 -                              <$> toJiraInlines xs
164 +        Link attr xs tgt   -> toJiraLink attr tgt xs
165          Math mtype cs      -> mathToJira mtype cs
166          Note bs            -> registerNotes bs
167          Quoted qt xs       -> quotedToJira qt xs
168 @@ -242,6 +242,28 @@ imageToJira (_, classes, kvs) src title =
169    Right xs -> xs
170    Left _  -> singleton $ Jira.Str t
171  
172 +-- | Creates a Jira Link element.
173 +toJiraLink :: PandocMonad m
174 +           => Attr
175 +           -> Target
176 +           -> [Inline]
177 +           -> JiraConverter m [Jira.Inline]
178 +toJiraLink (_, classes, _) (url, _) alias = do
179 +  let (linkType, url') = toLinkType url
180 +  description <- if url `elem` [stringify alias, "mailto:" <> stringify alias]
181 +                 then pure mempty
182 +                 else toJiraInlines alias
183 +  pure . singleton $ Jira.Link linkType description (Jira.URL url')
184 + where
185 +  toLinkType url'
186 +    | Just email <- T.stripPrefix "mailto:" url' = (Jira.Email, email)
187 +    | "user-account" `elem` classes              = (Jira.User, dropTilde url)
188 +    | "attachment" `elem` classes                = (Jira.Attachment, url)
189 +    | otherwise                                  = (Jira.External, url)
190 +  dropTilde txt = case T.uncons txt of
191 +    Just ('~', username) -> username
192 +    _                    -> txt
193 +
194  mathToJira :: PandocMonad m
195             => MathType
196             -> Text
197 diff --git a/stack.yaml b/stack.yaml
198 index 4ff8c8e258..524bc945a5 100644
199 --- a/stack.yaml
200 +++ b/stack.yaml
201 @@ -20,7 +20,7 @@ extra-deps:
202  - regex-pcre-builtin-0.95.0.8.8.35
203  - doclayout-0.3
204  - emojis-0.1
205 -- jira-wiki-markup-1.1.3
206 +- jira-wiki-markup-1.3.0
207  - HsYAML-0.2.0.0
208  - HsYAML-aeson-0.2.0.0
209  - doctemplates-0.8.1
210 diff --git a/test/Tests/Readers/Jira.hs b/test/Tests/Readers/Jira.hs
211 index 8e37968eb3..30f55585be 100644
212 --- a/test/Tests/Readers/Jira.hs
213 +++ b/test/Tests/Readers/Jira.hs
214 @@ -111,6 +111,10 @@ tests =
215        "HCO ~3~^-^" =?>
216        para ("HCO " <> subscript "3" <> superscript "-")
217  
218 +    , "citation" =:
219 +      "Et tu, Brute? ??Caesar??" =?>
220 +      para ("Et tu, Brute? — " <> emph "Caesar")
221 +
222      , "color" =:
223        "This is {color:red}red{color}." =?>
224        para ("This is " <> spanWith ("", [], [("color", "red")]) "red" <> ".")
225 @@ -123,9 +127,35 @@ tests =
226        "first\nsecond" =?>
227        para ("first" <> linebreak <> "second")
228  
229 -    , "link" =:
230 -      "[Example|https://example.org]" =?>
231 -      para (link "https://example.org" "" "Example")
232 +    , testGroup "links"
233 +      [ "external" =:
234 +        "[Example|https://example.org]" =?>
235 +        para (link "https://example.org" "" "Example")
236 +
237 +      , "email" =:
238 +        "[mailto:me@example.org]" =?>
239 +        para (link "mailto:me@example.org" "" "me@example.org")
240 +
241 +      , "email with description" =:
242 +        "[email|mailto:me@example.org]" =?>
243 +        para (link "mailto:me@example.org" "" "email")
244 +
245 +      , "attachment" =:
246 +        "[^example.txt]" =?>
247 +        para (linkWith ("", ["attachment"], []) "example.txt" "" "example.txt")
248 +
249 +      , "attachment with description" =:
250 +        "[an example^example.txt]" =?>
251 +        para (linkWith ("", ["attachment"], []) "example.txt" "" "an example")
252 +
253 +      , "user" =:
254 +        "[~johndoe]" =?>
255 +        para (linkWith ("", ["user-account"], []) "~johndoe" "" "~johndoe")
256 +
257 +      , "user with description" =:
258 +        "[John Doe|~johndoe]" =?>
259 +        para (linkWith ("", ["user-account"], []) "~johndoe" "" "John Doe")
260 +      ]
261  
262      , "image" =:
263        "!https://example.com/image.jpg!" =?>
This page took 0.069541 seconds and 3 git commands to generate.