diff --git a/CHANGES.rst b/CHANGES.rst index 6b40628..e65ef47 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,3 +5,8 @@ Version log ----- - Initial version + +0.1.1 +----- + +- Fixed a bug where putting a ``:`` or escaped ``>`` or ``<`` just before or after some color formatted text would raise a ``ColorSyntaxError`` \ No newline at end of file diff --git a/README.rst b/README.rst index e1ef7c1..414fa58 100644 --- a/README.rst +++ b/README.rst @@ -1,4 +1,4 @@ -colorise v0.1.0 +colorise v0.1.1 ================ .. image:: https://travis-ci.org/MisanthropicBit/colorise.svg?branch=master @@ -64,7 +64,7 @@ of returning a string: >>> colorise.highlight("Highlight this text!", fg='blue', indices=[4, 17, 3, 5, 10]) If you have a ``<`` or ``>`` in your string, you can escape it with a backslash ``\``. The backslash -is automatically removed. Colons ``:`` are ignored if they appear in text. +is automatically removed. Colons, escaped or not, ``:`` are ignored if they appear as text. .. code:: diff --git a/colorise/ColorFormatParser.py b/colorise/ColorFormatParser.py index 4fa6a82..b705a2f 100644 --- a/colorise/ColorFormatParser.py +++ b/colorise/ColorFormatParser.py @@ -2,7 +2,7 @@ """ColorFormatParser class which parses color formatted strings.""" -__date__ = '2014-05-08' # YYYY-MM-DD +__date__ = '2014-05-24' # YYYY-MM-DD import re import itertools @@ -37,10 +37,11 @@ def tokenize(self, string): pos, buf, lm = -1, '', -1 it = colorise.compat.ifilter(None, self._pattern.finditer(string)) t = colorise.compat.next(it) + escapeflag = False # Check if we need to yield any starting text if t.start() > 0: - yield string[:t.start()] + yield string[:t.start()], False pos = t.start() it = itertools.chain([t], it) @@ -52,26 +53,29 @@ def tokenize(self, string): if escaped: buf += string[pos:m.end(2)-1] + s + escapeflag = True else: buf += string[pos:m.start(3)] if buf: - yield buf + yield buf, escapeflag buf = '' + escapeflag = False if lm == start: - yield '' + yield '', False - yield s + yield s, False lm = m.end() pos = m.end() if buf: - yield buf + yield buf, escapeflag + escapeflag = False if pos < len(string): - yield string[pos:] + yield string[pos:], False def parse(self, format_string): """Parse color syntax from a formatted string.""" @@ -79,27 +83,27 @@ def parse(self, format_string): colorstack = [(None, None)] itokens = self.tokenize(format_string) - for token in itokens: - if token == self._START_TOKEN: + for token, escaped in itokens: + if token == self._START_TOKEN and not escaped: if txt: yield txt, colorstack[-1] txt = '' state += 1 - colors = self.extract_syntax(colorise.compat.next(itokens)) + colors = self.extract_syntax(colorise.compat.next(itokens)[0]) colorstack.append(tuple(b or a for a, b in zip(colorstack[-1], colors))) - elif token == self._FMT_TOKEN: - if state == 0: - raise ColorSyntaxError("Missing '{0}'" - .format(self._START_TOKEN)) + elif token == self._FMT_TOKEN and not escaped: + # if state == 0: + # raise ColorSyntaxError("Missing '{0}'" + # .format(self._START_TOKEN)) if state % 2 != 0: state += 1 else: txt += token - elif token == self._STOP_TOKEN: + elif token == self._STOP_TOKEN and not escaped: if state < 2: raise ColorSyntaxError("Missing '{0}' or '{1}'" .format(self._STOP_TOKEN, diff --git a/colorise/__init__.py b/colorise/__init__.py index 8644317..fe6859d 100644 --- a/colorise/__init__.py +++ b/colorise/__init__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -"""colorise v0.1.0. +"""colorise v0.1.1. A module for printing colored text to the console. Has support for custom color format syntax and includes some useful utility functions. @@ -10,9 +10,9 @@ from __future__ import print_function __author__ = 'Alexander Bock' -__version__ = '0.1.0' +__version__ = '0.1.1' __license__ = 'MIT' -__date__ = '2014-05-08' # YYYY-MM-DD +__date__ = '2014-05-21' # YYYY-MM-DD __all__ = ['set_color', 'cprint', 'fprint', 'formatcolor', 'formatbyindex', 'highlight']