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
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

A really simple text / keyword highlighter

#1 Post by kivik »

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

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

label start:
    "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!"
Edit: Changed from default to defined - the class isn't save file friendly but I don't see the need for it being saved.
Last edited by kivik on Tue May 08, 2018 6:10 am, edited 1 time in total.

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

Re: A really simple text / keyword highlighter

#2 Post by philat »

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

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: A really simple text / keyword highlighter

#3 Post by Remix »

Would it not be as easy to use the {=style_name} tag?

Code: Select all

style red:
    color "#E22"
    size 24

label start:
    "... {=red}red bit{/=}"
Only supports antialias, font, size, bold, italic, underline, strikethrough, color, black_color and kerning though.
Frameworks & Scriptlets:

User avatar
ComputerArt.Club
Veteran
Posts: 427
Joined: Mon May 22, 2017 8:12 am
Completed: Famous Fables, BoPoMoFo: Learn Chinese, Santa's workshop, Cat's Bath, Computer Art Club
Location: Taiwan
Contact:

Re: A really simple text / keyword highlighter

#4 Post by ComputerArt.Club »

I'd like to animate the highlighting of words in sync with an audio recording (I can get the timing from audacity). Haven't researched it yet. Any ideas (besides using images)?

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: A really simple text / keyword highlighter

#5 Post by Remix »

ComputerArt.Club wrote: Fri Apr 27, 2018 9:41 am I'd like to animate the highlighting of words in sync with an audio recording (I can get the timing from audacity). Haven't researched it yet. Any ideas (besides using images)?
Probably better to ask in the non Cookbook forum ;)
Now you've got me thinking about Ren'py Karaoke...
Frameworks & Scriptlets:

User avatar
ComputerArt.Club
Veteran
Posts: 427
Joined: Mon May 22, 2017 8:12 am
Completed: Famous Fables, BoPoMoFo: Learn Chinese, Santa's workshop, Cat's Bath, Computer Art Club
Location: Taiwan
Contact:

Re: A really simple text / keyword highlighter

#6 Post by ComputerArt.Club »

Remix wrote: Fri Apr 27, 2018 9:55 am Probably better to ask in the non Cookbook forum ;)
Now you've got me thinking about Ren'py Karaoke...
Yeah, might do that later, but I haven't got to that part of the game yet, other features to implement first. That might ecen end up in a later game instead.
Déjà vu, I feel like we have discussed the karaoke thing before. Yes, that is just one possible use for it. I want to highlight the words as they are read (my current games are for children and toddlers). I'd like to add songs too, but its early days yet.
Last edited by ComputerArt.Club on Wed May 09, 2018 12:04 pm, edited 1 time in total.

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: A really simple text / keyword highlighter

#7 Post by kivik »

philat wrote: 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.
Oops, I never checked back on this forum, and fun enough I know the cause from a different part of my game that used the same mechanics. I shouldn't have created them with defaults - they should only be defined as they're not meant to be saved. But I didn't know that when I make the cookbook!

Will edit.

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: A really simple text / keyword highlighter

#8 Post by kivik »

Remix wrote: Fri Apr 27, 2018 7:01 am Would it not be as easy to use the {=style_name} tag?

Code: Select all

style red:
    color "#E22"
    size 24

label start:
    "... {=red}red bit{/=}"
Only supports antialias, font, size, bold, italic, underline, strikethrough, color, black_color and kerning though.
My reasoning was to skip the closing tags all the time when I keep needing to style stuff. When I discovered @property and __getattr__ in python I just thought I could navigate around that really quickly.

But it's really cool to know you can apply text style this way! Obviously your approach is more powerful in that it can use underscores but mine can't!

Post Reply

Who is online

Users browsing this forum: No registered users