1.1 --- a/moinformat/output/common.py Mon Jul 30 01:03:50 2018 +0200
1.2 +++ b/moinformat/output/common.py Mon Jul 30 17:18:52 2018 +0200
1.3 @@ -23,17 +23,44 @@
1.4
1.5 "A common output context abstraction."
1.6
1.7 - def __init__(self):
1.8 + default_encoding = "utf-8"
1.9 +
1.10 + def __init__(self, parameters=None):
1.11
1.12 - "Initialise the output context."
1.13 + "Initialise the output context with the optional 'parameters'."
1.14 +
1.15 + self.parameters = parameters
1.16 + self.encoding = parameters and parameters.get("encoding") or self.default_encoding
1.17 +
1.18 + # Set up an output collector.
1.19
1.20 self.output = []
1.21 - self.out = self.output.append
1.22 +
1.23 + def encode(self, text):
1.24 +
1.25 + "Encode 'text' using the configured encoding."
1.26 +
1.27 + return encode(text, self.encoding)
1.28 +
1.29 + def out(self, text):
1.30 +
1.31 + "Add 'text' to the output collector."
1.32 +
1.33 + self.output.append(self.encode(text))
1.34
1.35 def to_string(self):
1.36
1.37 - "Return the output as a string."
1.38 + "Return the output as a plain string."
1.39
1.40 return "".join(self.output)
1.41
1.42 +def encode(s, encoding):
1.43 +
1.44 + "Encode 's' using 'encoding' if Unicode."
1.45 +
1.46 + if isinstance(s, unicode):
1.47 + return s.encode(encoding)
1.48 + else:
1.49 + return s
1.50 +
1.51 # vim: tabstop=4 expandtab shiftwidth=4