From eb184d9148e8a3e8c896a71550b1f0bee8da9a21 Mon Sep 17 00:00:00 2001
From: Albert Krewinkel <albert@zeitkraut.de>
Date: Mon, 8 Mar 2021 12:40:25 +0100
Subject: [PATCH] Jira writer: use noformat instead of code for unknown
 languages.

Code blocks that are not marked as a language supported by Jira are
rendered as preformatted text with `{noformat}` blocks.

Fixes: tarleb/jira-wiki-markup#4
---
 src/Text/Pandoc/Writers/Jira.hs | 10 +++---
 test/Tests/Writers/Jira.hs      | 10 ++++++
 test/writer.jira                | 55 +++++++++++++--------------------
 3 files changed, 37 insertions(+), 38 deletions(-)

diff --git a/src/Text/Pandoc/Writers/Jira.hs b/src/Text/Pandoc/Writers/Jira.hs
index 131896201..a714dac2e 100644
--- a/src/Text/Pandoc/Writers/Jira.hs
+++ b/src/Text/Pandoc/Writers/Jira.hs
@@ -126,13 +126,13 @@ toJiraCode :: PandocMonad m
            -> Text
            -> JiraConverter m [Jira.Block]
 toJiraCode (ident, classes, _attribs) code = do
-  let lang = case find (\c -> T.toLower c `elem` knownLanguages) classes of
-               Nothing -> Jira.Language "java"
-               Just l  -> Jira.Language l
   let addAnchor b = if T.null ident
                     then b
                     else [Jira.Para (singleton (Jira.Anchor ident))] <> b
-  return . addAnchor . singleton $ Jira.Code lang mempty code
+  return . addAnchor . singleton $
+    case find (\c -> T.toLower c `elem` knownLanguages) classes of
+      Nothing -> Jira.NoFormat mempty code
+      Just l  -> Jira.Code (Jira.Language l) mempty code
 
 -- | Creates a Jira definition list
 toJiraDefinitionList :: PandocMonad m
@@ -310,7 +310,7 @@ registerNotes contents = do
 knownLanguages :: [Text]
 knownLanguages =
   [ "actionscript", "ada", "applescript", "bash", "c", "c#", "c++"
-  , "css", "erlang", "go", "groovy", "haskell", "html", "javascript"
+  , "css", "erlang", "go", "groovy", "haskell", "html", "java", "javascript"
   , "json", "lua", "nyan", "objc", "perl", "php", "python", "r", "ruby"
   , "scala", "sql", "swift", "visualbasic", "xml", "yaml"
   ]
diff --git a/test/Tests/Writers/Jira.hs b/test/Tests/Writers/Jira.hs
index aff8348d4..b618c3970 100644
--- a/test/Tests/Writers/Jira.hs
+++ b/test/Tests/Writers/Jira.hs
@@ -68,5 +68,15 @@ tests =
         spanWith ("unicorn", [], []) (str "Unicorn") =?>
         "{anchor:unicorn}Unicorn"
       ]
+
+    , testGroup "code"
+      [ "code block with known language" =:
+        codeBlockWith ("", ["java"], []) "Book book = new Book(\"Algebra\")" =?>
+        "{code:java}\nBook book = new Book(\"Algebra\")\n{code}"
+
+      , "code block without language" =:
+        codeBlockWith ("", [], []) "preformatted\n  text.\n" =?>
+        "{noformat}\npreformatted\n  text.\n{noformat}"
+      ]
     ]
   ]
diff --git a/test/writer.jira b/test/writer.jira
index aff0dc320..71b720d65 100644
--- a/test/writer.jira
+++ b/test/writer.jira
@@ -33,11 +33,10 @@ bq. This is a block quote. It is pretty short.
 {quote}
 Code in a block quote:
 
-{code:java}
+{noformat}
 sub status {
     print "working";
-}
-{code}
+}{noformat}
 A list:
 
 # item one
@@ -56,22 +55,20 @@ And a following paragraph.
 h1. {anchor:code-blocks}Code Blocks
 Code:
 
-{code:java}
+{noformat}
 ---- (should be four hyphens)
 
 sub status {
     print "working";
 }
 
-this code block is indented by one tab
-{code}
+this code block is indented by one tab{noformat}
 And:
 
-{code:java}
+{noformat}
     this code block is indented by two tabs
 
-These should not be escaped:  \$ \\ \> \[ \{
-{code}
+These should not be escaped:  \$ \\ \> \[ \{{noformat}
 ----
 h1. {anchor:lists}Lists
 h2. {anchor:unordered}Unordered
@@ -236,9 +233,8 @@ red fruit
 contains seeds, crisp, pleasant to taste
 * *_orange_*
 orange fruit
-{code:java}
-{ orange code block }
-{code}
+{noformat}
+{ orange code block }{noformat}
 bq. orange block quote
 
 Multiple definitions, tight:
@@ -292,16 +288,14 @@ foo
 
 This should be a code block, though:
 
-{code:java}
+{noformat}
 <div>
     foo
-</div>
-{code}
+</div>{noformat}
 As should this:
 
-{code:java}
-<div>foo</div>
-{code}
+{noformat}
+<div>foo</div>{noformat}
 Now, nested:
 
 foo
@@ -312,16 +306,14 @@ Multiline:
 
 Code block:
 
-{code:java}
-<!-- Comment -->
-{code}
+{noformat}
+<!-- Comment -->{noformat}
 Just plain comment, with trailing spaces on the line:
 
 Code:
 
-{code:java}
-<hr />
-{code}
+{noformat}
+<hr />{noformat}
 Hr’s:
 
 ----
@@ -478,9 +470,8 @@ Indented [thrice|/url].
 
 This should \[not\]\[\] be a link.
 
-{code:java}
-[not]: /url
-{code}
+{noformat}
+[not]: /url{noformat}
 Foo [bar|/url/].
 
 Foo [biz|/url/].
@@ -506,9 +497,8 @@ An e-mail address: [mailto:nobody@nowhere.net]
 bq. Blockquoted: [http://example.com/]
 Auto-links should not occur here: {{<http://example.com/>}}
 
-{code:java}
-or here: <http://example.com/>
-{code}
+{noformat}
+or here: <http://example.com/>{noformat}
 ----
 h1. {anchor:images}Images
 From "Voyage dans la Lune" by Georges Melies \(1902):
@@ -534,9 +524,8 @@ This paragraph should not be part of the note, as it is not indented.
 
 Subsequent blocks are indented to show that they belong to the footnote \(as with list items).
 
-{code:java}
-  { <code> }
-{code}
+{noformat}
+  { <code> }{noformat}
 If you want, you can indent every line, but you can also be lazy and just indent the first line of each block.