git.iko.soy/typeable/pandoc
on master help sign in
Lua filter: use lua strings for nullary constructors

Lua string are used to represent nullary data constructors. The previous table-based representation was based on the JSON serialization, but can be simplified. This also matches the way those arguments are passed to custom writers.

commitd671b69b8790a0c73019257ec2d8cd21859e1c06 authorAlbert Krewinkel <albert@zeitkraut.de> dateFri 14 Apr 2017, 22:32 (10 years ago) parent0add425
2 files changed+41−124
data/pandoc.lua+22−44 src/Text/Pandoc/Lua/StackInstances.hs+19−80
data/pandoc.lua+22−44
@@ -565,115 +565,93 @@ M.Superscript = M.Inline:create_constructor(
565565
566566--- Math content is to be displayed on a separate line.
567567-- @see Math
568−M.DisplayMath = {}
569−M.DisplayMath.t = "DisplayMath"
568+M.DisplayMath = "DisplayMath"
570569--- Math content is to be displayed inline within the paragraph
571570-- @see Math
572−M.InlineMath = {}
573−M.InlineMath.t = "InlineMath"
571+M.InlineMath = "InlineMath"
574572
575573--- Double quoted content.
576574-- @see Quoted
577−M.DoubleQuote = {}
578−M.DoubleQuote.t = "DoubleQuote"
575+M.DoubleQuote = "DoubleQuote"
579576
580577--- Single quoted content.
581578-- @see Quoted
582−M.SingleQuote = {}
583−M.SingleQuote.t = "SingleQuote"
579+M.SingleQuote = "SingleQuote"
584580
585581--- Author name is mentioned in the text.
586582-- @see Citation
587583-- @see Cite
588−M.AuthorInText = {}
589−M.AuthorInText.t = "AuthorInText"
584+M.AuthorInText = "AuthorInText"
590585
591586--- Author name is suppressed.
592587-- @see Citation
593588-- @see Cite
594−M.SuppressAuthor = {}
595−M.SuppressAuthor.t = "SuppressAuthor"
589+M.SuppressAuthor = "SuppressAuthor"
596590
597591--- Default citation style is used.
598592-- @see Citation
599593-- @see Cite
600−M.NormalCitation = {}
601−M.NormalCitation.t = "NormalCitation"
594+M.NormalCitation = "NormalCitation"
602595
603596--- Table cells aligned left.
604597-- @see Table
605−M.AlignLeft = {}
606−M.AlignLeft.t = "AlignLeft"
598+M.AlignLeft = "AlignLeft"
607599
608600--- Table cells right-aligned.
609601-- @see Table
610−M.AlignRight = {}
611−M.AlignRight.t = "AlignRight"
602+M.AlignRight = "AlignRight"
612603
613604--- Table cell content is centered.
614605-- @see Table
615−M.AlignCenter = {}
616−M.AlignCenter.t = "AlignCenter"
606+M.AlignCenter = "AlignCenter"
617607
618608--- Table cells are alignment is unaltered.
619609-- @see Table
620−M.AlignDefault = {}
621−M.AlignDefault.t = "AlignDefault"
610+M.AlignDefault = "AlignDefault"
622611
623612--- Default list number delimiters are used.
624613-- @see OrderedList
625−M.DefaultDelim = {}
626−M.DefaultDelim.t = "DefaultDelim"
614+M.DefaultDelim = "DefaultDelim"
627615
628616--- List numbers are delimited by a period.
629617-- @see OrderedList
630−M.Period = {}
631−M.Period.t = "Period"
618+M.Period = "Period"
632619
633620--- List numbers are delimited by a single parenthesis.
634621-- @see OrderedList
635−M.OneParen = {}
636−M.OneParen.t = "OneParen"
622+M.OneParen = "OneParen"
637623
638624--- List numbers are delimited by a double parentheses.
639625-- @see OrderedList
640−M.TwoParens = {}
641−M.TwoParens.t = "TwoParens"
626+M.TwoParens = "TwoParens"
642627
643628--- List are numbered in the default style
644629-- @see OrderedList
645−M.DefaultStyle = {}
646−M.DefaultStyle.t = "DefaultStyle"
630+M.DefaultStyle = "DefaultStyle"
647631
648632--- List items are numbered as examples.
649633-- @see OrderedList
650−M.Example = {}
651−M.Example.t = "Example"
634+M.Example = "Example"
652635
653636--- List are numbered using decimal integers.
654637-- @see OrderedList
655−M.Decimal = {}
656−M.Decimal.t = "Decimal"
638+M.Decimal = "Decimal"
657639
658640--- List are numbered using lower-case roman numerals.
659641-- @see OrderedList
660−M.LowerRoman = {}
661−M.LowerRoman.t = "LowerRoman"
642+M.LowerRoman = "LowerRoman"
662643
663644--- List are numbered using upper-case roman numerals
664645-- @see OrderedList
665−M.UpperRoman = {}
666−M.UpperRoman.t = "UpperRoman"
646+M.UpperRoman = "UpperRoman"
667647
668648--- List are numbered using lower-case alphabetic characters.
669649-- @see OrderedList
670−M.LowerAlpha = {}
671−M.LowerAlpha.t = "LowerAlpha"
650+M.LowerAlpha = "LowerAlpha"
672651
673652--- List are numbered using upper-case alphabetic characters.
674653-- @see OrderedList
675−M.UpperAlpha = {}
676−M.UpperAlpha.t = "UpperAlpha"
654+M.UpperAlpha = "UpperAlpha"
677655
678656
679657------------------------------------------------------------------------
src/Text/Pandoc/Lua/StackInstances.hs+19−80
@@ -34,10 +34,11 @@ module Text.Pandoc.Lua.StackInstances () where
3434
3535import Control.Applicative ( (<|>) )
3636import Scripting.Lua
37− ( LTYPE(..), LuaState, StackValue(..), getglobal2, ltype, newtable, objlen )
37+ ( LTYPE(..), LuaState, StackValue(..), ltype, newtable, objlen )
3838import Text.Pandoc.Definition
3939import Text.Pandoc.Lua.SharedInstances ()
4040import Text.Pandoc.Lua.Util ( addValue, getTable, pushViaConstructor )
41+import Text.Pandoc.Shared ( safeRead )
4142
4243instance StackValue Pandoc where
4344 push lua (Pandoc meta blocks) = do
@@ -143,19 +144,8 @@ instance StackValue Inline where
143144 valuetype _ = TTABLE
144145
145146instance StackValue Alignment where
146− push lua = \case
147− AlignLeft -> getglobal2 lua "pandoc.AlignLeft"
148− AlignRight -> getglobal2 lua "pandoc.AlignRight"
149− AlignCenter -> getglobal2 lua "pandoc.AlignCenter"
150− AlignDefault -> getglobal2 lua "pandoc.AlignDefault"
151− peek lua idx = do
152− tag <- getTable lua idx "t"
153− case tag of
154− Just "AlignLeft" -> return $ Just AlignLeft
155− Just "AlignRight" -> return $ Just AlignRight
156− Just "AlignCenter" -> return $ Just AlignCenter
157− Just "AlignDefault" -> return $ Just AlignDefault
158− _ -> return Nothing
147+ push lua = push lua . show
148+ peek lua idx = (>>= safeRead) <$> peek lua idx
159149 valuetype _ = TSTRING
160150
161151instance StackValue Citation where
@@ -172,18 +162,9 @@ instance StackValue Citation where
172162 valuetype _ = TTABLE
173163
174164instance StackValue CitationMode where
175− push lua = \case
176− AuthorInText -> getglobal2 lua "pandoc.AuthorInText"
177− NormalCitation -> getglobal2 lua "pandoc.NormalCitation"
178− SuppressAuthor -> getglobal2 lua "pandoc.SuppressAuthor"
179− peek lua idx = do
180− tag <- getTable lua idx "t"
181− case tag of
182− Just "AuthorInText" -> return $ Just AuthorInText
183− Just "NormalCitation" -> return $ Just NormalCitation
184− Just "SuppressAuthor" -> return $ Just SuppressAuthor
185− _ -> return Nothing
186− valuetype _ = TTABLE
165+ push lua = push lua . show
166+ peek lua idx = (>>= safeRead) <$> peek lua idx
167+ valuetype _ = TSTRING
187168
188169instance StackValue Format where
189170 push lua (Format f) = push lua f
@@ -191,66 +172,24 @@ instance StackValue Format where
191172 valuetype _ = TSTRING
192173
193174instance StackValue ListNumberDelim where
194− push lua = \case
195− DefaultDelim -> getglobal2 lua "pandoc.DefaultDelim"
196− Period -> getglobal2 lua "pandoc.Period"
197− OneParen -> getglobal2 lua "pandoc.OneParen"
198− TwoParens -> getglobal2 lua "pandoc.TwoParens"
199− peek lua idx = do
200− tag <- getTable lua idx "t"
201− case tag of
202− Just "DefaultDelim" -> return $ Just DefaultDelim
203− Just "Period" -> return $ Just Period
204− Just "OneParen" -> return $ Just OneParen
205− Just "TwoParens" -> return $ Just TwoParens
206− _ -> return Nothing
207− valuetype _ = TTABLE
175+ push lua = push lua . show
176+ peek lua idx = (>>= safeRead) <$> peek lua idx
177+ valuetype _ = TSTRING
208178
209179instance StackValue ListNumberStyle where
210− push lua = \case
211− DefaultStyle -> getglobal2 lua "pandoc.DefaultStyle"
212− LowerRoman -> getglobal2 lua "pandoc.LowerRoman"
213− UpperRoman -> getglobal2 lua "pandoc.UpperRoman"
214− LowerAlpha -> getglobal2 lua "pandoc.LowerAlpha"
215− UpperAlpha -> getglobal2 lua "pandoc.UpperAlpha"
216− Decimal -> getglobal2 lua "pandoc.Decimal"
217− Example -> getglobal2 lua "pandoc.Example"
218− peek lua idx = do
219− tag <- getTable lua idx "t"
220− case tag of
221− Just "DefaultStyle" -> return $ Just DefaultStyle
222− Just "LowerRoman" -> return $ Just LowerRoman
223− Just "UpperRoman" -> return $ Just UpperRoman
224− Just "LowerAlpha" -> return $ Just LowerAlpha
225− Just "UpperAlpha" -> return $ Just UpperAlpha
226− Just "Decimal" -> return $ Just Decimal
227− Just "Example" -> return $ Just Example
228− _ -> return Nothing
229− valuetype _ = TTABLE
180+ push lua = push lua . show
181+ peek lua idx = (>>= safeRead) <$> peek lua idx
182+ valuetype _ = TSTRING
230183
231184instance StackValue MathType where
232− push lua = \case
233− InlineMath -> getglobal2 lua "pandoc.InlineMath"
234− DisplayMath -> getglobal2 lua "pandoc.DisplayMath"
235− peek lua idx = do
236− res <- getTable lua idx "t"
237− case res of
238− Just "InlineMath" -> return $ Just InlineMath
239− Just "DisplayMath" -> return $ Just DisplayMath
240− _ -> return Nothing
241− valuetype _ = TTABLE
185+ push lua = push lua . show
186+ peek lua idx = (>>= safeRead) <$> peek lua idx
187+ valuetype _ = TSTRING
242188
243189instance StackValue QuoteType where
244− push lua = \case
245− SingleQuote -> getglobal2 lua "pandoc.SingleQuote"
246− DoubleQuote -> getglobal2 lua "pandoc.DoubleQuote"
247− peek lua idx = do
248− res <- getTable lua idx "t"
249− case res of
250− Just "SingleQuote" -> return $ Just SingleQuote
251− Just "DoubleQuote" -> return $ Just DoubleQuote
252− _ -> return Nothing
253− valuetype _ = TTABLE
190+ push lua = push lua . show
191+ peek lua idx = (>>= safeRead) <$> peek lua idx
192+ valuetype _ = TSTRING
254193
255194-- | Return the value at the given index as inline if possible.
256195peekInline :: LuaState -> Int -> IO (Maybe Inline)