Not logged inGosu Forums
Forum back to Help Search Register Login
Up Topic Gosu / Gosu Exchange / Suggestions on how to implement syntax highlighting
- - By misbehavens Date 2013-08-22 23:23
I'm working on a project where I would like to use Gosu to display some code on the screen as text with syntax highlighting. I'm not sure how to approach this. I just started using Gosu::Image.from_text to display the code, using a monospaced font. This works okay so far, but I noticed that it does not respect indentation at the beginning of a line. Also this function assumes you want to display the text as white. I know there are a lot of libraries to turn some text into syntax highlighted HTML, but not sure if any are flexible enough to return something that I can use to create text/colors compatible with Gosu. Do you have any ideas/suggestions for me?
Parent - - By lol_o2 Date 2013-08-23 11:52
Gosu does support HTML tags itself.
Put some <c=rrggbb> in your string and it will be parsed in color.
Parent - - By misbehavens Date 2013-08-23 17:35
Ah, you are totally right! This worked: "Make this text <c=ff0000>red</c>". Although that's doesn't look like any HTML I've ever seen. ;)
Parent - - By Spooner Date 2013-08-24 01:46
They are XML, at least ;)

You should be able to convince one of the code highlighting libraries to spit out gosu-compatible formatting by adapting the existing parsers (e.g. coderay).
Parent - By misbehavens Date 2013-08-24 21:35
Yeah I was able to modify the CodeRay HTML output to make it work. I suppose writing my own CodeRay encoder would have been a bit cleaner, but this works for now:

html = CodeRay.scan(text, language).span(line_numbers: :inline)

# remove unsupported properties
html.gsub! '<span class="CodeRay">', ''
html.gsub! /<\/span>\z/, ''
html.gsub! /font-weight:bold/, ''

# convert inline styles to Gosu compatible color tags
html.gsub! /<span style="background-color:hsla.*?">/, '<c=FFFFFF>'
html.gsub! /<span class="line-numbers">(?<space> )?(<strong>)?<a href="#n\d+?" name="n\d+?">(?<number>\d+?)<\/a>(<\/strong>)?(<\/span>)?/, '\k<space>\k<number> '
html.gsub! /<span style="color:#([0-9a-fA-F]{3});?">/ do |m|
  color = ${|s|s*2}.join
html.gsub! '</span>', '</c>'
html.gsub! '&quot;', '"'
Parent - - By EdwinOdesseiron Date 2013-09-09 12:05
Personally I think this should be included in Gosu documentation. I had no idea I can use XML tags, and it really made my life easier. By the time it was posted I had a separate line colouring in chatlog system implemented, and I was wondering how to implement a single word colouring. It saved me hours of coding and trying various approaches.
Parent - - By jlnr (dev) Date 2013-09-09 15:50
The problem with these features is that I don't know where to document them. I think Gosu::Font supports this feature too, so should I paste the same paragraph in Image#from_text and Font#draw? rdoc/yard are not exactly helpful for this kind of stuff. :( I agree though, it's just hard to get started with all the documentation that's stlll pending...
Parent - By EdwinOdesseiron Date 2013-09-09 16:14
I guess these 2 places (Image#from_text and Font#draw) are suitable for this. Small note that it allows the use of XML's <c>,<u>,<i> and what else tags. Definitely better than no information at all, I'd say.
Parent - By erisdiscord Date 2013-09-13 18:49 Edited 2013-09-13 18:59
Document it on Font#draw; add a "see also" to Image#from_text with a note saying that that method supports the same formatting codes.

Also, you should note in the docs that this is not actually XML or HTML so XML-style nesting rules don't apply—e.g., <b>bold <i>bold-italic</b> italic</i> is perfectly valid.
Parent - By misbehavens Date 2013-08-24 21:36
FYI: In case anyone is interested, this is what I've been working on lately:
Up Topic Gosu / Gosu Exchange / Suggestions on how to implement syntax highlighting

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill