def test_macro1(**kwargs): """ >>> test_macro1(foo="bar") "[test macro1 - kwargs: foo='bar']" >>> test_macro1() '[test macro1 - kwargs: ]' >>> test_macro1(a=1,b=2) '[test macro1 - kwargs: a=1,b=2]' """ kwargs = ','.join(['%s=%s' % (k, repr2(v)) for k, v in sorted(kwargs.items())]) return "[test macro1 - kwargs: %s]" % kwargs
def dict2string(d): """ FIXME: Find a better was to do this. >>> dict2string({'foo':"bar", "no":123}) "foo='bar' no=123" >>> dict2string({"foo":'bar', "no":"ABC"}) "foo='bar' no='ABC'" See test_creole2html.TestDict2String() """ attr_list = [] for key, value in sorted(d.items()): value_string = repr2(value) attr_list.append("%s=%s" % (key, value_string)) return " ".join(attr_list)
def macro_emit(self, node): #print(node.debug()) macro_name = node.macro_name text = node.content macro = None args = node.macro_args try: macro_kwargs = string2dict(args) except ValueError as e: exc_info = sys.exc_info() return self.error( "Wrong macro arguments: %s for macro '%s' (maybe wrong macro tag syntax?)" % ( repr2(args), macro_name ), exc_info ) macro_kwargs["text"] = text if callable(self.macros) == True: raise DeprecationWarning("Callable macros are not supported anymore!") return exc_info = None if isinstance(self.macros, dict): try: macro = self.macros[macro_name] except KeyError as e: exc_info = sys.exc_info() else: try: macro = getattr(self.macros, macro_name) except AttributeError as e: exc_info = sys.exc_info() if macro == None: return self.error( "Macro '%s' doesn't exist" % macro_name, exc_info ) try: result = macro(**macro_kwargs) except TypeError as err: msg = "Macro '%s' error: %s" % (macro_name, err) exc_info = sys.exc_info() if self.verbose > 1: # Inject more information about the macro in traceback etype, evalue, etb = exc_info import inspect filename = inspect.getfile(macro) try: sourceline = inspect.getsourcelines(macro)[0][0].strip() except IOError as err: evalue = etype("%s (error getting sourceline: %s from %s)" % (evalue, err, filename)) else: evalue = etype("%s (sourceline: %r from %s)" % (evalue, sourceline, filename)) exc_info = etype, evalue, etb return self.error(msg, exc_info) except Exception as err: return self.error( "Macro '%s' error: %s" % (macro_name, err), exc_info=sys.exc_info() ) if not isinstance(result, TEXT_TYPE): msg = "Macro '%s' doesn't return a unicode string!" % macro_name if self.verbose > 1: msg += " - returns: %r, type %r" % (result, type(result)) return self.error(msg) if node.kind == "macro_block": result += "\n" return result