From cfed4cc7deaf34fca4be144b00efc634737476ee Mon Sep 17 00:00:00 2001 From: "Thomas \"Cakeisalie5\" Touhey" Date: Fri, 22 Jun 2018 01:35:41 +0200 Subject: [PATCH] Going forward! --- TAGS.rst | 6 +++-- test/test_html.py | 11 +++++--- textoutpc/builtin_tags/Show.py | 5 ++-- textoutpc/translate.py | 48 +++++++++++++++++++++++++--------- 4 files changed, 50 insertions(+), 20 deletions(-) diff --git a/TAGS.rst b/TAGS.rst index b384049..84c2e3c 100644 --- a/TAGS.rst +++ b/TAGS.rst @@ -24,8 +24,10 @@ There are a few public members you can define as a tag: It is defined as ``True`` by default for all tags; - ``notempty``: ignore the tag when its content is empty. By default, this value is `False`; -- ``superblock``: is a super-block (for blocks), adds a paragraph - tag implicitely. +- ``superblock``: is a super-block (for blocks) which means it adds a block + level, and adds a paragraph tag implicitely. +- ``inlined``: if is a block, transforms automatically the surrounding block + into a superblock while it's there. So for example, if I want to make the inline tag ``[hello]`` as an example, with the alternate name ``[hai]``, I'd start off by writing: diff --git a/test/test_html.py b/test/test_html.py index aa516be..4417c6d 100755 --- a/test/test_html.py +++ b/test/test_html.py @@ -47,14 +47,17 @@ __test_cases = { '

b

', 'a[i]k[center][b]b[justify]c[/center]d[/]wouhou': \ '

ak

' \ - '

b

' \ - '

c

' \ + '

b

' \ + '

c

' \ '

dwouhou

', # Show tag for super preprocessing blocks. + '[show]lol': '

lol

', '[quote][show][justify]hehe': \ - '

<div class="align-justify">' \ - '<p>hehe</p></div>

', + '

' \ + '<div class="align-justify">' \ + '<p>hehe</p></div>' \ + '

', # Titles. 'lolk[title]smth': '

lolk

' '

smth

', diff --git a/textoutpc/builtin_tags/Show.py b/textoutpc/builtin_tags/Show.py index 9abc13c..5562ebf 100755 --- a/textoutpc/builtin_tags/Show.py +++ b/textoutpc/builtin_tags/Show.py @@ -18,6 +18,7 @@ class TextoutShowTag(_TextoutBlockTag): aliases = ('[show]',) superblock = True + inlined = True generic = False raw = False @@ -25,9 +26,9 @@ class TextoutShowTag(_TextoutBlockTag): return _htmlescape(content) def begin_html(self): - return '' + return '

' def end_html(self): - return '' + return '

' # End of file. diff --git a/textoutpc/translate.py b/textoutpc/translate.py index 9a183ec..3ecdbe2 100755 --- a/textoutpc/translate.py +++ b/textoutpc/translate.py @@ -83,11 +83,12 @@ class _TagData: # Flags and properties calculated from the tag's attributes, using the # rules given in `TAGS.md`. # `ign` is whether the content should be read while the tag is opened. - # `raw` is whether the tag's content should be read as raw. # `generic` is whether the tag can be terminated by the generic # tag ending mark [/]. - # `notempty` is whether the tag should be used with an empty - # content or not (e.g. to avoid `

`), True if not. + # `raw` is whether the tag's content should be read as raw. + # `super` is whether the tag is a superblock or not. + # `inlined` is whether the next block on the same level is turned into + # a superblock or not. self.ign = not hasattr(tag, 'preprocess') and hasattr(tag, 'content') @@ -101,6 +102,9 @@ class _TagData: bool(tag.superblock) if hasattr(tag, 'superblock') \ else False + self.inlined = bool(tag.inlined) if self.super \ + and hasattr(tag, 'inlined') and bool(tag.inlined) else False + # Content processing utilities. # `last` is the content of the tag. A boolean indicates that we # only want to know if the content is empty or not, and a string @@ -126,7 +130,7 @@ class _TagData: self.last = "" def __repr__(self): - return '' + return '' # --- # Translator main class. @@ -217,7 +221,8 @@ class Translator: self.text_group += text - def flush_text(self): + def flush_text(self, superblocks_only = False, + next_block_is_super = False): """ Flush the text that has been output. """ # First of all, check if the text group is empty or if we want to @@ -233,13 +238,16 @@ class Translator: text = self.text_group self.text_group = "" - self.add_text(text, process_func = lambda x: self.process_text(x)) + self.add_text(text, process_func = lambda x: self.process_text(x), + superblocks_only = superblocks_only, + next_block_is_super = next_block_is_super) # --- # Code outputting utilities. # --- - def add_text(self, text, process_func = lambda x: x, start_tags = True): + def add_text(self, text, process_func = lambda x: x, start_tags = True, + superblocks_only = False, next_block_is_super = False): """ Add text to the higher blocks if available. """ # The last queue is composed of booleans (does the group contain @@ -254,8 +262,12 @@ class Translator: # Check if it is a tag we want to contribute to. if dat.type == dat.BLOCK: - if dat.super or not blockfound: + if dat.super or next_block_is_super: blockfound = True + next_block_is_super = dat.inlined + elif not superblocks_only and not blockfound: + blockfound = True + next_block_is_super = False else: continue @@ -290,7 +302,8 @@ class Translator: self.outp.write(message) - def put_code(self, code, start_tags = True): + def put_code(self, code, start_tags = True, + superblocks_only = True, next_block_is_super = False): """ Put some code. """ # We don't want to mix text and code, so we'll flush to be sure that @@ -305,7 +318,9 @@ class Translator: # Add the code. - self.add_text(code, start_tags = start_tags) + self.add_text(code, start_tags = start_tags, + superblocks_only = superblocks_only, + next_block_is_super = next_block_is_super) def put_newline(self): """ Put a newline. """ @@ -373,6 +388,10 @@ class Translator: dat = self.queue.pop(0) tag = dat.tag + pcattrs = {'start_tags': False, + 'superblocks_only': dat.type == dat.BLOCK, + 'next_block_is_super': dat.inlined} + # If preprocessing has been enabled, we ought to process the content, # check if the tag is valid, and do everything we would have done # while pushing the tag if it didn't do content processing. @@ -493,6 +512,7 @@ class Translator: block_to_end = None inlines = [] + next_block_is_super = False for dat in self.queue: # Check if the tag hasn't already been started or doesn't call # for content processing. @@ -505,12 +525,15 @@ class Translator: # Then put the tag in the appropriate queue. if dat.type == dat.BLOCK: - if dat.super: + if dat.super or next_block_is_super: superblocks.insert(0, dat) + next_block_is_super = dat.inlined elif dat.started: block_to_end = dat + next_block_is_super = dat.inlined elif block_to_end == None and block_to_start == None: block_to_start = dat + next_block_is_super = dat.inlined else: inlines.insert(0, dat) @@ -683,7 +706,8 @@ class Translator: # Push a paragraph tag if the block is a superblock. - if dat.type == dat.BLOCK and dat.super and type(dat.last) == bool: + if dat.type == dat.BLOCK and dat.super and not dat.raw \ + and not dat.inlined: self.push_tag(_TagData(_TextoutParagraphTag(None, None, self.output_type, self.tweaks), None, ''))