From 845b6c8670a6d1a11c1a31ec15ec2f0831883cbe Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Tue, 28 Nov 2017 13:35:49 -0800
Subject: [PATCH] Added --print-highlight-style option.

This generates a JSON version of a highlighting style, which can be
saved as a .theme file, modified, and used with `--highlight-style`.

Closes #4106.
Closes #4096.
---
 MANUAL.txt             | 12 ++++++++++--
 src/Text/Pandoc/App.hs | 27 ++++++++++++++++++++++++++-
 2 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/MANUAL.txt b/MANUAL.txt
index c22d18bac..e8fdf9375 100644
--- a/MANUAL.txt
+++ b/MANUAL.txt
@@ -685,8 +685,16 @@ General writer options
     Instead of a *STYLE* name, a JSON file with extension
     `.theme` may be supplied.  This will be parsed as a KDE
     syntax highlighting theme and (if valid) used as the
-    highlighting style.  To see a sample theme that can be
-    modified, `pandoc --print-default-data-file default.theme`.
+    highlighting style.
+
+    To generate the JSON version of an existing style,
+    use `--print-highlight-style`.
+
+`--print-highlight-style=`*STYLE*|*FILE*
+
+:   Prints a JSON version of a highlighting style, which can
+    be modified, saved with a `.theme` extension, and used
+    with `--highlight-style`.
 
 `--syntax-definition=`*FILE*
 
diff --git a/src/Text/Pandoc/App.hs b/src/Text/Pandoc/App.hs
index 3c52e4247..f1c21c69a 100644
--- a/src/Text/Pandoc/App.hs
+++ b/src/Text/Pandoc/App.hs
@@ -63,7 +63,10 @@ import qualified Data.Yaml as Yaml
 import GHC.Generics
 import Network.URI (URI (..), parseURI)
 import Paths_pandoc (getDataDir)
-import Skylighting (Style, Syntax (..), defaultSyntaxMap, parseTheme)
+import Data.Aeson.Encode.Pretty (encodePretty', Config(..), keyOrder,
+         defConfig, Indent(..), NumberFormat(..))
+import Skylighting (Style, Syntax (..), defaultSyntaxMap, parseTheme,
+                    pygments)
 import Skylighting.Parser (addSyntaxDefinition, missingIncludes,
                            parseSyntaxDefinition)
 import System.Console.GetOpt
@@ -1047,6 +1050,28 @@ options =
                   "FILE")
                   "" -- "Print default data file"
 
+    , Option "" ["print-highlight-style"]
+                 (ReqArg
+                  (\arg _ -> do
+                     sty <- fromMaybe pygments <$>
+                              lookupHighlightStyle (Just arg)
+                     B.putStr $ encodePretty'
+                       defConfig{confIndent = Spaces 4
+                                ,confCompare = keyOrder
+                                  (map T.pack
+                                   ["text-color"
+                                   ,"background-color"
+                                   ,"line-numbers"
+                                   ,"bold"
+                                   ,"italic"
+                                   ,"underline"
+                                   ,"text-styles"])
+                                ,confNumFormat = Generic
+                                ,confTrailingNewline = True} sty
+                     exitSuccess)
+                  "STYLE|FILE")
+                 "" -- "Print default template for FORMAT"
+
     , Option "" ["dpi"]
                  (ReqArg
                   (\arg opt ->