PageRenderTime 80ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Text/Pandoc/Lua/Marshal/WriterOptions.hs

https://github.com/jgm/pandoc
Haskell | 244 lines | 171 code | 38 blank | 35 comment | 15 complexity | b3ab18e8c30b29fc8913000dc70e5699 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. {-# LANGUAGE CPP #-}
  2. {-# LANGUAGE LambdaCase #-}
  3. {-# LANGUAGE OverloadedStrings #-}
  4. {-# LANGUAGE ScopedTypeVariables #-}
  5. {-# OPTIONS_GHC -fno-warn-orphans #-}
  6. {- |
  7. Module : Text.Pandoc.Lua.Marshaling.WriterOptions
  8. Copyright : © 2021-2022 Albert Krewinkel, John MacFarlane
  9. License : GNU GPL, version 2 or above
  10. Maintainer : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
  11. Stability : alpha
  12. Marshaling instance for WriterOptions and its components.
  13. -}
  14. module Text.Pandoc.Lua.Marshal.WriterOptions
  15. ( peekWriterOptions
  16. , pushWriterOptions
  17. ) where
  18. import Control.Applicative (optional)
  19. import Data.Default (def)
  20. import HsLua as Lua
  21. #if !MIN_VERSION_hslua(2,2,0)
  22. import HsLua.Aeson (peekViaJSON, pushViaJSON)
  23. #endif
  24. import Text.Pandoc.Lua.Marshal.List (pushPandocList)
  25. import Text.Pandoc.Lua.Marshal.Template (peekTemplate, pushTemplate)
  26. import Text.Pandoc.Options (WriterOptions (..))
  27. --
  28. -- Writer Options
  29. --
  30. -- | Retrieve a WriterOptions value, either from a normal WriterOptions
  31. -- value, from a read-only object, or from a table with the same
  32. -- keys as a WriterOptions object.
  33. peekWriterOptions :: LuaError e => Peeker e WriterOptions
  34. peekWriterOptions = retrieving "WriterOptions" . \idx ->
  35. liftLua (ltype idx) >>= \case
  36. TypeUserdata -> peekUD typeWriterOptions idx
  37. TypeTable -> peekWriterOptionsTable idx
  38. _ -> failPeek =<<
  39. typeMismatchMessage "WriterOptions userdata or table" idx
  40. -- | Pushes a WriterOptions value as userdata object.
  41. pushWriterOptions :: LuaError e => Pusher e WriterOptions
  42. pushWriterOptions = pushUD typeWriterOptions
  43. -- | 'WriterOptions' object type.
  44. typeWriterOptions :: LuaError e => DocumentedType e WriterOptions
  45. typeWriterOptions = deftype "WriterOptions"
  46. [ operation Tostring $ lambda
  47. ### liftPure show
  48. <#> udparam typeWriterOptions "opts" "options to print in native format"
  49. =#> functionResult pushString "string" "Haskell representation"
  50. ]
  51. [ property "cite_method"
  52. "How to print cites"
  53. (pushViaJSON, writerCiteMethod)
  54. (peekViaJSON, \opts x -> opts{ writerCiteMethod = x })
  55. , property "columns"
  56. "Characters in a line (for text wrapping)"
  57. (pushIntegral, writerColumns)
  58. (peekIntegral, \opts x -> opts{ writerColumns = x })
  59. , property "dpi"
  60. "DPI for pixel to/from inch/cm conversions"
  61. (pushIntegral, writerDpi)
  62. (peekIntegral, \opts x -> opts{ writerDpi = x })
  63. , property "email_obfuscation"
  64. "How to obfuscate emails"
  65. (pushViaJSON, writerEmailObfuscation)
  66. (peekViaJSON, \opts x -> opts{ writerEmailObfuscation = x })
  67. , property "epub_chapter_level"
  68. "Header level for chapters (separate files)"
  69. (pushIntegral, writerEpubChapterLevel)
  70. (peekIntegral, \opts x -> opts{ writerEpubChapterLevel = x })
  71. , property "epub_fonts"
  72. "Paths to fonts to embed"
  73. (pushPandocList pushString, writerEpubFonts)
  74. (peekList peekString, \opts x -> opts{ writerEpubFonts = x })
  75. , property "epub_metadata"
  76. "Metadata to include in EPUB"
  77. (maybe pushnil pushText, writerEpubMetadata)
  78. (optional . peekText, \opts x -> opts{ writerEpubMetadata = x })
  79. , property "epub_subdirectory"
  80. "Subdir for epub in OCF"
  81. (pushText, writerEpubSubdirectory)
  82. (peekText, \opts x -> opts{ writerEpubSubdirectory = x })
  83. , property "extensions"
  84. "Markdown extensions that can be used"
  85. (pushViaJSON, writerExtensions)
  86. (peekViaJSON, \opts x -> opts{ writerExtensions = x })
  87. , property "highlight_style"
  88. "Style to use for highlighting (nil = no highlighting)"
  89. (maybe pushnil pushViaJSON, writerHighlightStyle)
  90. (optional . peekViaJSON, \opts x -> opts{ writerHighlightStyle = x })
  91. , property "html_math_method"
  92. "How to print math in HTML"
  93. (pushViaJSON, writerHTMLMathMethod)
  94. (peekViaJSON, \opts x -> opts{ writerHTMLMathMethod = x })
  95. , property "html_q_tags"
  96. "Use @<q>@ tags for quotes in HTML"
  97. (pushBool, writerHtmlQTags)
  98. (peekBool, \opts x -> opts{ writerHtmlQTags = x })
  99. , property "identifier_prefix"
  100. "Prefix for section & note ids in HTML and for footnote marks in markdown"
  101. (pushText, writerIdentifierPrefix)
  102. (peekText, \opts x -> opts{ writerIdentifierPrefix = x })
  103. , property "incremental"
  104. "True if lists should be incremental"
  105. (pushBool, writerIncremental)
  106. (peekBool, \opts x -> opts{ writerIncremental = x })
  107. , property "listings"
  108. "Use listings package for code"
  109. (pushBool, writerListings)
  110. (peekBool, \opts x -> opts{ writerListings = x })
  111. , property "number_offset"
  112. "Starting number for section, subsection, ..."
  113. (pushPandocList pushIntegral, writerNumberOffset)
  114. (peekList peekIntegral, \opts x -> opts{ writerNumberOffset = x })
  115. , property "number_sections"
  116. "Number sections in LaTeX"
  117. (pushBool, writerNumberSections)
  118. (peekBool, \opts x -> opts{ writerNumberSections = x })
  119. , property "prefer_ascii"
  120. "Prefer ASCII representations of characters when possible"
  121. (pushBool, writerPreferAscii)
  122. (peekBool, \opts x -> opts{ writerPreferAscii = x })
  123. , property "reference_doc"
  124. "Path to reference document if specified"
  125. (maybe pushnil pushString, writerReferenceDoc)
  126. (optional . peekString, \opts x -> opts{ writerReferenceDoc = x })
  127. , property "reference_links"
  128. "Use reference links in writing markdown, rst"
  129. (pushBool, writerReferenceLinks)
  130. (peekBool, \opts x -> opts{ writerReferenceLinks = x })
  131. , property "reference_location"
  132. "Location of footnotes and references for writing markdown"
  133. (pushViaJSON, writerReferenceLocation)
  134. (peekViaJSON, \opts x -> opts{ writerReferenceLocation = x })
  135. , property "section_divs"
  136. "Put sections in div tags in HTML"
  137. (pushBool, writerSectionDivs)
  138. (peekBool, \opts x -> opts{ writerSectionDivs = x })
  139. , property "setext_headers"
  140. "Use setext headers for levels 1-2 in markdown"
  141. (pushBool, writerSetextHeaders)
  142. (peekBool, \opts x -> opts{ writerSetextHeaders = x })
  143. , property "slide_level"
  144. "Force header level of slides"
  145. (maybe pushnil pushIntegral, writerSlideLevel)
  146. (optional . peekIntegral, \opts x -> opts{ writerSlideLevel = x })
  147. -- , property "syntax_map" "Syntax highlighting definition"
  148. -- (pushViaJSON, writerSyntaxMap)
  149. -- (peekViaJSON, \opts x -> opts{ writerSyntaxMap = x })
  150. -- :: SyntaxMap
  151. , property "tab_stop"
  152. "Tabstop for conversion btw spaces and tabs"
  153. (pushIntegral, writerTabStop)
  154. (peekIntegral, \opts x -> opts{ writerTabStop = x })
  155. , property "table_of_contents"
  156. "Include table of contents"
  157. (pushBool, writerTableOfContents)
  158. (peekBool, \opts x -> opts{ writerTableOfContents = x })
  159. , property "template"
  160. "Template to use"
  161. (maybe pushnil pushTemplate, writerTemplate)
  162. (optional . peekTemplate, \opts x -> opts{ writerTemplate = x })
  163. -- :: Maybe (Template Text)
  164. , property "toc_depth"
  165. "Number of levels to include in TOC"
  166. (pushIntegral, writerTOCDepth)
  167. (peekIntegral, \opts x -> opts{ writerTOCDepth = x })
  168. , property "top_level_division"
  169. "Type of top-level divisions"
  170. (pushViaJSON, writerTopLevelDivision)
  171. (peekViaJSON, \opts x -> opts{ writerTopLevelDivision = x })
  172. , property "variables"
  173. "Variables to set in template"
  174. (pushViaJSON, writerVariables)
  175. (peekViaJSON, \opts x -> opts{ writerVariables = x })
  176. , property "wrap_text"
  177. "Option for wrapping text"
  178. (pushViaJSON, writerWrapText)
  179. (peekViaJSON, \opts x -> opts{ writerWrapText = x })
  180. ]
  181. -- | Retrieves a 'WriterOptions' object from a table on the stack, using
  182. -- the default values for all missing fields.
  183. --
  184. -- Internally, this pushes the default writer options, sets each
  185. -- key/value pair of the table in the userdata value, then retrieves the
  186. -- object again. This will update all fields and complain about unknown
  187. -- keys.
  188. peekWriterOptionsTable :: LuaError e => Peeker e WriterOptions
  189. peekWriterOptionsTable idx = retrieving "WriterOptions (table)" $ do
  190. liftLua $ do
  191. absidx <- absindex idx
  192. pushUD typeWriterOptions def
  193. let setFields = do
  194. next absidx >>= \case
  195. False -> return () -- all fields were copied
  196. True -> do
  197. pushvalue (nth 2) *> insert (nth 2)
  198. settable (nth 4) -- set in userdata object
  199. setFields
  200. pushnil -- first key
  201. setFields
  202. peekUD typeWriterOptions top `lastly` pop 1
  203. instance Pushable WriterOptions where
  204. push = pushWriterOptions