A really simple text / keyword highlighter

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
kivik
Veteran
Posts: 224
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

A really simple text / keyword highlighter

#1 Post by kivik » Mon Apr 16, 2018 6:26 am

So I'm starting to write the content of my game and I figured highlighting keywords would be quite useful. However being as I'm lazy I created a very simple textParser to automatically add the text formatting. I'm still a novice at python and renpy so I'd welcome any improvements / feedbacks!

The class:

Code: Select all

init python:
    class textParser(object):
        def __init__(self, open_tag, close_tag):
            self._open = open_tag
            self._close = close_tag

        def __getattr__(self, text):
            return self._open + text.replace("_", " ") + self._close
To use it:

Code: Select all

default keyword = textParser("{color="+gui.accent_color+"}", "{/color}") # just changing the colour for instance
default emKeyword = textParser("{b}{color="+gui.accent_color+"}", "{/color}{/b}") # We can add more opening and closing tags if we want
default ts = object()

label start:
    # not sure how to set this as default
    $ ts.keyword = keyword
    $ ts.emphasis = emKeyword
    
    "We can now highlight the [keyword.Keyword] easily."
    "We can even highlight [keyword.Multiple_Words] by using underscore instead of space"
    "By using the other [emKeyword.TextParser] we can have different effects!"
    "You can even make a [ts.keyword.textStyle_object] and nest the [ts.emphasis.textParsers] inside for a range of parsers!"

philat
Eileen-Class Veteran
Posts: 1293
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: A really simple text / keyword highlighter

#2 Post by philat » Tue Apr 24, 2018 1:42 am

I thought this looked interesting and gave it a spin -- it causes errors on save/load/reloading in dev mode. I don't know enough about what goes on under the hood in save/load to offer an answer why.

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "renpy/common/00keymap.rpy", line 411, in script
    python hide:
  File "renpy/common/00keymap.rpy", line 411, in <module>
    python hide:
  File "renpy/common/00keymap.rpy", line 430, in _execute_python_hide
    renpy.save("_reload-1", "reload save game")
TypeError: 'unicode' object is not callable (perhaps store.keyword = <store.textParser object at 0x05DA1F50>)

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "renpy/common/00keymap.rpy", line 411, in script
    python hide:
  File "D:\Apps\renpy-6.99.12.4-sdk\renpy\ast.py", line 862, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "D:\Apps\renpy-6.99.12.4-sdk\renpy\python.py", line 1888, in py_exec_bytecode
    exec bytecode in globals, locals
  File "renpy/common/00keymap.rpy", line 411, in <module>
    python hide:
  File "renpy/common/00keymap.rpy", line 430, in _execute_python_hide
    renpy.save("_reload-1", "reload save game")
  File "D:\Apps\renpy-6.99.12.4-sdk\renpy\loadsave.py", line 402, in save
    dump((roots, renpy.game.log), logf)
  File "D:\Apps\renpy-6.99.12.4-sdk\renpy\loadsave.py", line 46, in dump
    cPickle.dump(o, f, cPickle.HIGHEST_PROTOCOL)
TypeError: 'unicode' object is not callable (perhaps store.keyword = <store.textParser object at 0x05DA1F50>)

Windows-7-6.1.7601-SP1
Ren'Py 6.99.14.3.3347
test 1.0
Tue Apr 24 14:41:07 2018

Post Reply

Who is online

Users browsing this forum: No registered users