Skip to content

Commit 4221337

Browse files
committed
import cleanup and commentating.
1 parent e9efa6f commit 4221337

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

textile/core.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020

2121
import uuid
2222
import six
23+
from six.moves.urllib_parse import (urlparse, urlsplit, urlunsplit, quote,
24+
unquote)
2325

2426
from textile.tools import sanitizer, imagesize
25-
from textile.regex_strings import (align_re_s, cls_re_s, halign_re_s,
26-
pnct_re_s, regex_snippets, syms_re_s, table_span_re_s, valign_re_s)
27+
from textile.regex_strings import (align_re_s, cls_re_s, pnct_re_s,
28+
regex_snippets, syms_re_s, table_span_re_s)
2729
from textile.utils import (decode_high, encode_high, encode_html, generate_tag,
2830
has_raw_text, is_rel_url, is_valid_url, list_type, normalize_newlines,
2931
parse_attributes, pba)
@@ -35,10 +37,6 @@
3537
except ImportError:
3638
from ordereddict import OrderedDict
3739

38-
from six.moves import urllib
39-
urlparse, urlsplit, urlunsplit, quote, unquote = (urllib.parse.urlparse,
40-
urllib.parse.urlsplit, urllib.parse.urlunsplit, urllib.parse.quote,
41-
urllib.parse.unquote)
4240

4341
try:
4442
import regex as re
@@ -430,6 +428,8 @@ def block(self, text):
430428
# split list
431429
text = re.split(r'(\n{2,})', text)
432430

431+
# some blocks, when processed, will ask us to output nothing, if that's
432+
# the case, we'd want to drop the whitespace which comes after it.
433433
eat_whitespace = False
434434

435435
tag = 'p'
@@ -438,6 +438,7 @@ def block(self, text):
438438
out = []
439439

440440
for line in text:
441+
# the line is just whitespace, add it to the output, and move on
441442
if not line.strip():
442443
if not eat_whitespace:
443444
out.append(line)
@@ -454,6 +455,8 @@ def block(self, text):
454455
# if we had a previous extended tag but not this time, close up
455456
# the tag
456457
if ext and out:
458+
# it's out[-2] because the last element in out is the
459+
# whitespace that preceded this line
457460
content = encode_html(out[-2], quotes=True)
458461
content = generate_tag(block.inner_tag, content,
459462
block.inner_atts)
@@ -478,14 +481,20 @@ def block(self, text):
478481
# no tag specified
479482
else:
480483
# if we're inside an extended block, add the text from the
481-
# previous extension to the front
484+
# previous line to the front
482485
if ext and out:
483486
line = '{0}{1}'.format(out.pop(), line)
484487
# the logic in the if statement below is a bit confusing in
485488
# php-textile. I'm still not sure I understand what the php
486-
# code is doing.
489+
# code is doing. Something tells me it's a phpsadness. Anyway,
490+
# this works, and is much easier to understand: if we're not in
491+
# an extension, and the line doesn't begin with a space, treat
492+
# it like a block to insert. Lines that begin with a space are
493+
# not processed as a block.
487494
if not ext and not line[0] == ' ':
488495
block = Block(self, tag, atts, ext, cite, line)
496+
# if the block contains html tags, generate_tag would
497+
# mangle it, so process as is.
489498
if block.tag == 'p' and not has_raw_text(block.content):
490499
line = block.content
491500
else:
@@ -498,23 +507,31 @@ def block(self, text):
498507
line = self.doPBr(line)
499508
line = line.replace('<br>', '<br />')
500509

510+
# if we're in an extended block, and we haven't specified a new
511+
# tag, join this line to the last item of the output
501512
if ext and not match:
502513
last_item = out.pop()
503514
out.append('{0}{1}'.format(last_item, line))
504515
elif not block.eat:
516+
# or if it's a type of block which indicates we shouldn't drop
517+
# it, add it to the output.
505518
out.append(line)
506519

507520
if not ext:
508521
tag = 'p'
509522
atts = ''
510523
cite = ''
511524

525+
# if it's a block we should drop, don't keep the whitespace which
526+
# will come after it.
512527
if block.eat:
513528
eat_whitespace = True
514529

530+
# at this point, we've gone through all the lines, and if there's still
531+
# an extension in effect, we close it here.
515532
if ext and out:
516-
out.append(generate_tag(block.outer_tag, out.pop(),
517-
block.outer_atts))
533+
final = generate_tag(block.outer_tag, out.pop(), block.outer_atts)
534+
out.append(final)
518535
return ''.join(out)
519536

520537
def footnoteRef(self, text):

0 commit comments

Comments
 (0)