diff options
| author | Sean Brant | 2013-03-14 22:17:56 -0500 |
|---|---|---|
| committer | Sean Brant | 2013-03-14 22:17:56 -0500 |
| commit | b2ced5bc0413bcf03f6ec2e27552aa276445d0f0 (patch) | |
| tree | 353161b18b56fb9333275a2a320e816ddfa0f2c9 | |
| parent | dbd34828cc6370e7edd3819510cccc1295ff6813 (diff) | |
| download | pykss-b2ced5bc0413bcf03f6ec2e27552aa276445d0f0.tar.bz2 | |
Fixes indenting issues
| -rw-r--r-- | pykss/comment.py | 53 | ||||
| -rw-r--r-- | pykss/contrib/django/templatetags/pykss.py | 3 | ||||
| -rw-r--r-- | tests/fixtures/comments.txt | 11 | ||||
| -rw-r--r-- | tests/test_comment.py | 16 |
4 files changed, 58 insertions, 25 deletions
diff --git a/pykss/comment.py b/pykss/comment.py index 69e148b..5c9c705 100644 --- a/pykss/comment.py +++ b/pykss/comment.py @@ -1,42 +1,55 @@ -SINGLE_LINE = '//' -MULTI_LINE = '*' -MULTI_LINE_START = '/*' -MULTI_LINE_END = '*/' +import re + + +single_line_re = re.compile(r'^\s*\/\/') +single_line_strip_re = re.compile(r'\s*\/\/') + +multi_line_start_re = re.compile(r'^\s*\/\*') +multi_line_end_re = re.compile(r'.*\*\/') +multi_line_start_strip_re = re.compile(r'\s*\/\*') +multi_line_end_strip_re = re.compile(r'\*\/') +multi_line_middle_strip_re = re.compile(r'^(\s*\*+)') + +preceding_white_space_re = re.compile(r'^\s*') def is_single_line_comment(line): - return line.startswith(SINGLE_LINE) + return single_line_re.match(line) is not None def is_multi_line_comment_start(line): - return line.startswith(MULTI_LINE_START) + return multi_line_start_re.match(line) is not None def is_multi_line_comment_end(line): if is_single_line_comment(line): return False - return line.endswith(MULTI_LINE_END) + return multi_line_end_re.match(line) is not None def parse_single_line(line): - return line[len(SINGLE_LINE):].strip() + return single_line_strip_re.sub('', line).rstrip() def parse_multi_line(line): - if is_multi_line_comment_start(line): - line = line[len(MULTI_LINE_START):] + cleaned = multi_line_start_strip_re.sub('', line) + return multi_line_end_strip_re.sub('', cleaned).rstrip() - if is_multi_line_comment_end(line): - line = line[:-len(MULTI_LINE_END)] - if line.startswith(MULTI_LINE): - line = line[len(MULTI_LINE):] +def normalize(lines): + cleaned = [] + indents = [] - return line.strip() + for line in lines: + line = multi_line_middle_strip_re.sub('', line) + cleaned.append(line) + match = preceding_white_space_re.match(line) + if line: + indents.append(len(match.group())) + indent = min(indents) -def normalize(lines): - return '\n'.join(lines).strip() + return '\n'.join([line[indent:] for line in cleaned]).strip() class CommentParser(object): @@ -52,8 +65,7 @@ class CommentParser(object): with open(self.filename) as fileobj: for line in fileobj: - line = line.strip() - + # Parse single-line style if is_single_line_comment(line): parsed = parse_single_line(line) @@ -63,6 +75,7 @@ class CommentParser(object): current_block = [parsed] inside_single_line_block = True + # Prase multi-line style if is_multi_line_comment_start(line) or inside_multi_line_block: parsed = parse_multi_line(line) @@ -72,9 +85,11 @@ class CommentParser(object): current_block = [parsed] inside_multi_line_block = True + # End a multi-line block if detected if is_multi_line_comment_end(line): inside_multi_line_block = False + # Store the current block if we're done if is_single_line_comment(line) is False and inside_multi_line_block is False: if current_block: blocks.append(normalize(current_block)) diff --git a/pykss/contrib/django/templatetags/pykss.py b/pykss/contrib/django/templatetags/pykss.py index a711c5e..2e17e59 100644 --- a/pykss/contrib/django/templatetags/pykss.py +++ b/pykss/contrib/django/templatetags/pykss.py @@ -1,6 +1,5 @@ from django import template from django.template.loader import render_to_string -from django.utils.html import strip_spaces_between_tags register = template.Library() @@ -64,7 +63,7 @@ class BaseStyleguideNode(template.Node): for section in sections: context.update({'section': section}) html = render_to_string(template_name, context) - output.append(strip_spaces_between_tags(html)) + output.append(html) context.pop() return ''.join(output) diff --git a/tests/fixtures/comments.txt b/tests/fixtures/comments.txt index 37dcdf6..885887c 100644 --- a/tests/fixtures/comments.txt +++ b/tests/fixtures/comments.txt @@ -30,4 +30,13 @@ Indented comments: // Indented single-line comment. - /* Indented block comment. */
\ No newline at end of file + /* Indented block comment. */ + + +Comment with example: + +/* This comment has a indented example + * <div> + * <div></div> + * </div> + */ diff --git a/tests/test_comment.py b/tests/test_comment.py index ea47156..e1e94c6 100644 --- a/tests/test_comment.py +++ b/tests/test_comment.py @@ -19,10 +19,10 @@ class CommentMethodTestCase(unittest.TestCase): self.assertFalse(comment.is_multi_line_comment_end("nooooope")) def test_parses_the_single_line_comment_syntax(self): - self.assertEqual(comment.parse_single_line("// yuuuuup"), 'yuuuuup') + self.assertEqual(comment.parse_single_line("// yuuuuup"), ' yuuuuup') def test_parses_the_multi_line_comment_syntax(self): - self.assertEqual(comment.parse_multi_line('/* yuuuup */'), 'yuuuup') + self.assertEqual(comment.parse_multi_line('/* yuuuup */'), ' yuuuup') class CommentParserTestCase(unittest.TestCase): @@ -35,7 +35,8 @@ class CommentParserTestCase(unittest.TestCase): expected = """ This comment block has comment identifiers on every line. -Fun fact: this is Kyle's favorite comment syntax!""" +Fun fact: this is Kyle's favorite comment syntax! + """ self.assertTrue(expected.strip() in self.comments) def test_finds_block_style_comment_styles(self): @@ -63,3 +64,12 @@ It has stars at the begining of every line. def test_handles_indented_comments(self): self.assertTrue('Indented single-line comment.' in self.comments) self.assertTrue('Indented block comment.' in self.comments) + + def test_handles_indented_example(self): + expected = """ +This comment has a indented example +<div> + <div></div> +</div> + """ + self.assertTrue(expected.strip() in self.comments) |
