Skip to content

Commit

Permalink
Updated escaping rules in README.rst and fixed a format parsing error
Browse files Browse the repository at this point in the history
  • Loading branch information
MisanthropicBit committed Dec 12, 2019
1 parent 0ff6072 commit 2c24148
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Expand Up @@ -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``
4 changes: 2 additions & 2 deletions README.rst
@@ -1,4 +1,4 @@
colorise v0.1.0
colorise v0.1.1
================

.. image:: https://travis-ci.org/MisanthropicBit/colorise.svg?branch=master
Expand Down Expand Up @@ -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::
Expand Down
34 changes: 19 additions & 15 deletions colorise/ColorFormatParser.py
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -52,54 +53,57 @@ 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."""
txt, state = '', 0
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,
Expand Down
6 changes: 3 additions & 3 deletions 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.
Expand All @@ -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']

Expand Down

0 comments on commit 2c24148

Please sign in to comment.