def invoke(self, tex): if self.macroMode == Macro.MODE_END: res = self.ownerDocument.createElement('end' + self.tagName).invoke(tex) if res is None: return [res, EndGroup(' ')] return res + [EndGroup(' ')] params = [None] # Get optional argument, if needed nargs = self.nargs if self.opt is not None: nargs -= 1 params.append( tex.readArgument('[]', default=self.opt, parentNode=self, name='#%s' % len(params))) # Get mandatory arguments for i in range(nargs): params.append( tex.readArgument(parentNode=self, name='#%s' % len(params))) deflog.debug2('expanding %s %s', self.definition, params) output = [] if self.macroMode == Macro.MODE_BEGIN: output.append(BeginGroup(' ')) return output + expandDef(self.definition, params)
def expandDef(definition, params): # Walk through the definition and expand parameters if not definition: return [] output = [] definition = iter(definition) previous = '' for t in definition: # Expand parameters if t.catcode == Token.CC_PARAMETER: for t in definition: # Double '#' if t.catcode == Token.CC_PARAMETER: output.append(t) else: if params[int(t)] is not None: # This is a pretty bad hack, but `ifx' commands # need an argument to also be a token. So we # wrap them in a group here and let the # TeX parser convert the group to a token. if previous == 'ifx': output.append(BeginGroup(' ')) output.extend(params[int(t)]) output.append(EndGroup(' ')) else: output.extend(params[int(t)]) break # Just append other tokens to the output else: output.append(t) previous = t return output