2
0
Fork 0

Blocks logic starts to really work!

This commit is contained in:
Thomas Touhey 2018-02-12 08:32:02 +01:00
parent c713b8e48b
commit 3cb86f5205
No known key found for this signature in database
GPG key ID: 2ECEB0517AD947FB
4 changed files with 52 additions and 8 deletions

32
scripts/textout2lightscript Executable file
View file

@ -0,0 +1,32 @@
#!/usr/bin/env python3
""" textout to lightscript converter for the command line. """
import sys, argparse
import textoutpc
def parse_args():
""" Parse the arguments. """
ap = argparse.ArgumentParser(prog='textout2html',
description='Convert textout BBcode to HTML.')
ap.add_argument('-o', dest='output', default=sys.stdout,
type=lambda x: sys.stdout if x == '-' else open(x, 'w'),
help='the output source, stdout by default.')
ap.add_argument('input', nargs='?', default=sys.stdin,
type=lambda x: sys.stdin if x == '-' else open(x, 'r'),
help='the input source, stdin by default.')
args = ap.parse_args()
return args
def main():
""" Main function of the script. """
args = parse_args()
print(textoutpc.tolightscript(args.input.read()), file=args.output, end='')
if __name__ == '__main__':
# TODO: manage exceptions
main()
# End of file.

View file

@ -13,7 +13,7 @@ setup(name='textoutpc',
keywords='planète casio textout bbcode translator parser',
packages=find_packages(),
# scripts=['textout2html'], -- TODO integrate, I guess?
scripts=['textout2html', 'textout2lightscript'],
install_requires=['regex'],

View file

@ -5,6 +5,8 @@
from .functions import tohtml, tolightscript
__all__ = ["version", "tohtml", "tolightscript"]
version = "0.1"
# End of file.

View file

@ -449,11 +449,12 @@ class Translator:
if dat.notempty and not dat.started:
pass
else:
if hasattr(tag, 'end'):
self.put_code(tag.end())
if dat.type == dat.BLOCK:
self.queue.insert(0, dat)
self.end_block()
self.queue.pop(0)
elif hasattr(tag, 'end'):
self.put_code(tag.end())
# Disable raw mode if it was a raw tag (which means that it enabled it,
# as tags into raw tags cannot be processed).
@ -503,6 +504,8 @@ class Translator:
def end_block(self):
""" End the current block. """
queue = self.queue.copy()
# We want to collect inline and block tags, in the order they
# were inserted, reversed.
@ -512,19 +515,22 @@ class Translator:
# Check if the tag has been started and if it is a super
# block (which means we want to stop here).
if not isinstance(dat.last, bool) or dat.super: break
if dat.super: break
if not dat.started: continue
# Then put the tag in the appropriate queue.
if dat.type == dat.BLOCK:
blocks.append(dat)
blocks.insert(0, dat)
else:
inlines.append(dat)
inlines.insert(0, dat)
# Then we want to end the tags, and reset them in case we're going
# to use them.
for dat in inlines + blocks:
self.queue = inlines + blocks
while self.queue:
dat = self.queue.pop(0)
tag = dat.tag
if hasattr(tag, 'end'):
self.put_code(tag.end())
@ -533,6 +539,10 @@ class Translator:
dat.started = False
dat.last = False
# Restore the queue.
self.queue = queue
# ---
# Main function.
# ---