|
| 1 | +{-# LANGUAGE LambdaCase #-} |
| 2 | +{-# LANGUAGE OverloadedStrings #-} |
| 3 | +{-| |
| 4 | +Module : Text.Pandoc.Lua.Module.Highlighting |
| 5 | +Copyright : © 2025 Albert Krewinkel |
| 6 | +License : MIT |
| 7 | +Maintainer : Albert Krewinkel <[email protected]> |
| 8 | +
|
| 9 | +Lua module for basic image operations. |
| 10 | +-} |
| 11 | +module Text.Pandoc.Lua.Module.Highlighting ( |
| 12 | + -- * Module |
| 13 | + documentedModule |
| 14 | + |
| 15 | + -- ** Functions |
| 16 | + , style |
| 17 | + ) |
| 18 | +where |
| 19 | + |
| 20 | +import Prelude hiding (null) |
| 21 | +import Control.Applicative ((<|>)) |
| 22 | +import Data.Default (def) |
| 23 | +import Data.Maybe (fromMaybe) |
| 24 | +import Data.Version (makeVersion) |
| 25 | +import HsLua.Aeson (peekViaJSON, pushViaJSON) |
| 26 | +import HsLua.Core |
| 27 | +import HsLua.Marshalling |
| 28 | +import HsLua.Packaging |
| 29 | +import Text.Blaze.Html.Renderer.Text (renderHtml) |
| 30 | +import Text.Pandoc.Definition (Block(CodeBlock), Inline(Code)) |
| 31 | +import Text.Pandoc.Error (PandocError) |
| 32 | +import Text.Pandoc.Highlighting |
| 33 | + ( Style |
| 34 | + , formatANSI |
| 35 | + , formatConTeXtBlock |
| 36 | + , formatConTeXtInline |
| 37 | + , formatHtmlBlock |
| 38 | + , formatHtmlInline |
| 39 | + , formatLaTeXBlock |
| 40 | + , formatLaTeXInline |
| 41 | + , highlightingStyles |
| 42 | + , lookupHighlightingStyle |
| 43 | + , pygments |
| 44 | + , styleToConTeXt |
| 45 | + , styleToCss |
| 46 | + , styleToLaTeX |
| 47 | + ) |
| 48 | +import Text.Pandoc.Lua.Marshal.AST (peekBlockFuzzy, peekInlineFuzzy) |
| 49 | +import Text.Pandoc.Lua.Marshal.WriterOptions (peekWriterOptions) |
| 50 | +import Text.Pandoc.Lua.PandocLua (unPandocLua) |
| 51 | +import Text.Pandoc.Options |
| 52 | + ( WriterOptions (writerHighlightStyle, writerSyntaxMap) ) |
| 53 | + |
| 54 | +import qualified Data.Text as T |
| 55 | +import qualified Data.Text.Lazy as TL |
| 56 | +import qualified Text.Pandoc.Highlighting as HL |
| 57 | + |
| 58 | +-- | The @pandoc.image@ module specification. |
| 59 | +documentedModule :: Module PandocError |
| 60 | +documentedModule = Module |
| 61 | + { moduleName = "pandoc.highlighting" |
| 62 | + , moduleDescription = "Code highlighting" |
| 63 | + , moduleFields = fields |
| 64 | + , moduleFunctions = |
| 65 | + [ definitions `since` makeVersion [3, 8] |
| 66 | + , highlight `since` makeVersion [3, 8] |
| 67 | + , style `since` makeVersion [3, 8] |
| 68 | + ] |
| 69 | + , moduleOperations = [] |
| 70 | + , moduleTypeInitializers = [] |
| 71 | + } |
| 72 | + |
| 73 | +-- |
| 74 | +-- Fields |
| 75 | +-- |
| 76 | + |
| 77 | +-- | Exported fields. |
| 78 | +fields :: LuaError e => [Field e] |
| 79 | +fields = |
| 80 | + [ Field |
| 81 | + { fieldName = "styles" |
| 82 | + , fieldType = "{string,...}" |
| 83 | + , fieldDescription = "List of known code highlighting styles." |
| 84 | + , fieldPushValue = pushList (pushText . fst) highlightingStyles |
| 85 | + } |
| 86 | + ] |
| 87 | + |
| 88 | +-- |
| 89 | +-- Functions |
| 90 | +-- |
| 91 | + |
| 92 | +-- | Gets a highlighting style of the given name. |
| 93 | +style :: DocumentedFunction PandocError |
| 94 | +style = defun "style" |
| 95 | + ### (unPandocLua . lookupHighlightingStyle) |
| 96 | + <#> stringParam "name" "style name or path to theme file" |
| 97 | + =#> functionResult pushViaJSON "table" "style" |
| 98 | + #? "Returns the style definitions for a given style name.\ |
| 99 | + \\ |
| 100 | + \ If the name is a standard style, it is loaded amd returned;\ |
| 101 | + \ if it ends in `.theme`, attemts to load a KDE theme from the \ |
| 102 | + \ file path specified." |
| 103 | + |
| 104 | +definitions :: DocumentedFunction PandocError |
| 105 | +definitions = defun "definitions" |
| 106 | + ### (\sty format -> case T.toLower format of |
| 107 | + "context" -> pure $ styleToConTeXt sty |
| 108 | + "css" -> pure $ T.pack $ styleToCss sty |
| 109 | + "latex" -> pure $ styleToLaTeX sty |
| 110 | + _ -> failLua $ "Unsupported format: " <> T.unpack format) |
| 111 | + <#> parameter peekStyle "table|string" "style" "style table or style name" |
| 112 | + <#> textParam "format" "`'context'`, `'css'`, or `'latex'`" |
| 113 | + =#> functionResult pushText "string" "style definitions" |
| 114 | + #? "Generate highlighting definitions for the given format.\ |
| 115 | + \ For example, to generate CSS definitions for the *espresso* style,\ |
| 116 | + \ run `pandoc.highlighting.toformat('espresso', 'css')`." |
| 117 | + |
| 118 | +highlight :: DocumentedFunction PandocError |
| 119 | +highlight = defun "highlight" |
| 120 | + ### (\codeElement format mwopts -> do |
| 121 | + (attr, code, inline) <- |
| 122 | + case codeElement of |
| 123 | + Left (Code a c) -> pure (a, c, True) |
| 124 | + Right (CodeBlock a c) -> pure (a, c, False) |
| 125 | + _ -> failLua "Cannot highlight element" |
| 126 | + let wopts = fromMaybe def mwopts |
| 127 | + let sty = fromMaybe pygments (writerHighlightStyle wopts) |
| 128 | + (inlineFormatter, blockFormatter) <- case T.toLower format of |
| 129 | + "ansi" -> pure ( \opts lns -> formatANSI opts sty lns |
| 130 | + , \opts lns -> formatANSI opts sty lns ) |
| 131 | + "context" -> pure (formatConTeXtInline, formatConTeXtBlock) |
| 132 | + "html" -> let htmlToText fn = \opts src -> |
| 133 | + TL.toStrict $ renderHtml (fn opts src) |
| 134 | + in pure ( htmlToText formatHtmlInline |
| 135 | + , htmlToText formatHtmlBlock ) |
| 136 | + "latex" -> pure (formatLaTeXInline, formatLaTeXBlock) |
| 137 | + _ -> failLua $ |
| 138 | + "Unsupported highlighting format: " <> T.unpack format |
| 139 | + let syntaxMap = writerSyntaxMap wopts |
| 140 | + let formatter = if inline then inlineFormatter else blockFormatter |
| 141 | + case HL.highlight syntaxMap formatter attr code of |
| 142 | + Left err -> failLua $ T.unpack err |
| 143 | + Right result -> pure result) |
| 144 | + <#> parameter |
| 145 | + (\idx -> |
| 146 | + (Left <$> peekInlineFuzzy idx) <|> |
| 147 | + (Right <$> peekBlockFuzzy idx)) |
| 148 | + "Inline|Block" "code element" "element that will be highlighted" |
| 149 | + <#> textParam "format" |
| 150 | + "target format (`'ansi'`, `'context'`, `'html'`, or `'latex'`')" |
| 151 | + <#> opt (parameter peekWriterOptions "WriterOptions" "wopts" "") |
| 152 | + =#> functionResult pushText "string" "highlighted code" |
| 153 | + #? "Highlight code in the given format." |
| 154 | + |
| 155 | +-- | Retrieves a highlighting style; accepts a string, themepath, or style |
| 156 | +-- table. |
| 157 | +peekStyle :: Peeker PandocError Style |
| 158 | +peekStyle idx = do |
| 159 | + liftLua (ltype idx) >>= \case |
| 160 | + TypeTable -> peekViaJSON idx |
| 161 | + TypeString -> do |
| 162 | + name <- peekString idx |
| 163 | + liftLua $ unPandocLua $ lookupHighlightingStyle name |
| 164 | + _type -> failPeek "Can't retrieve style." |
0 commit comments