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
Posts: 217
Joined: Fri Jun 24, 2016 5:58 pm

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!"

Post Reply

Who is online

Users browsing this forum: No registered users