Rails erb.html highlighting (ruby embedded on HTML/js)

Request Features and Feature Changes

Rails erb.html highlighting (ruby embedded on HTML/js)

Postby egarcia » Tue Jul 28, 2009 7:55 am

Hi there,

Editra's got ruby syntax highlighting (for what I'm really thankful!) but it lacks support for .erb.html pages, like the ones used on rail's views.

These pages are quite similar to PHP pages, in which server code is "mixed up" with HTML (and sometimes javascript, too!)

On the following example, the parts that go <% like this %> or <%= like this %> are ruby server script. The rest is client html/javascript

Code: Select all
<% title "Cities" %>
<script language="javascript">
  // Some js code here, to make things interesting...
<script/>
<table>
  <tr>
    <th>Name</th>
    <th>Province</th>
  </tr>
  <% for city in @cities %>
    <tr>
      <td><%=h city.name %></td>
      <td><%=h city.province.name %></td>
      <td><%= link_to "Show", city %></td>
      <td><%= link_to "Edit", edit_city_path(city) %></td>
      <td><%= link_to "Destroy", city, :confirm => 'Are you sure?', :method => :delete %></td>
    </tr>
  <% end %>
</table>

<p><%= link_to "New City", new_city_path %></p>


It would be great if this could be included. I can try to implement this, if any indications on how to do it are provided - I don't know where to start.

I'm using windows xp.

Thanks a lot.

I'm using
egarcia
Limited User
 
Posts: 3
Joined: Tue Jul 28, 2009 7:08 am

Re: Rails erb.html highlighting (ruby embedded on HTML/js)

Postby cody » Tue Jul 28, 2009 12:01 pm

Hello,

Look in the Editra/src/syntax folder for examples. See the issuelist.py or xtext.py for examples of custom highlighters. Most highlighting is provided by Scintilla but I don't think it does not have built in support for the file type you showed.

Let me know if you have any other questions.


Cody
User avatar
cody
Site Admin
 
Posts: 1315
Joined: Mon Oct 09, 2006 2:49 am
Location: United States

Re: Rails erb.html highlighting (ruby embedded on HTML/js)

Postby egarcia » Wed Jul 29, 2009 9:59 am

Hi Cody,

I've had a look at the code.

I've never touched python before, and I have no way of testing the changes below, so please take them with a grain of salt.

What I've basically done is modifying the ruby.py file. The aim is adapting php.py's "HTML embedding" to ruby.

First change: include html on ruby.py:
Code: Select all
...
# Local Imports
import synglob
import html     # <-- new line
...


Second: modifiying the Keywords function
Code: Select all
def Keywords(lang_id=0):
    """Returns Specified Keywords List
    @param lang_id: used to select specific subset of keywords

    """
    if lang_id == synglob.ID_LANG_RUBY:
        # Support Embedded HTML highlighting.
        keywords = HTML_KEYWORDS
        keywords.append((4, RUBY_KW))
        return keywords # Original version returned RUBY_KW
    else:
        return list()


Third: Modifying SyntaxSpec
Code: Select all
def SyntaxSpec(lang_id=0):
    """Syntax Specifications
    @param lang_id: used for selecting a specific subset of syntax specs

    """
    if lang_id == synglob.ID_LANG_RUBY:
        return html.SYNTAX_ITEMS + SYNTAX_ITEMS # original returned SYNTAX_ITEMS
    else:
        return list()


Fourth: Modifying Properties. This is the one where I have most doubts: php.py just returns html.FOLD and html.FLD_HTML. I don't know whether I should have removed FOLD and TIMMY from there.
Code: Select all
def Properties(lang_id=0):
    """Returns a list of Extra Properties to set
    @param lang_id: used to select a specific set of properties

    """
    if lang_id == synglob.ID_LANG_RUBY:
        return [FOLD, TIMMY, html.FOLD, html.FLD_HTML] #original returned [FOLD, TIMMY]
    else:
        return list()


Finally, there are two missing changes changes that I don't know how to implement:
  • I could not find where the {'<%php', '%>'} symbols for changing context are defined on PHP. We would need to add something similar for ruby, but with { '<%', '%>' } instead.
  • The ruby list of ruby file extensions files should also include *.rhtml, *.erb and *.rjs. Or else we leave ruby like it is and we add a "rails" language instead

I hope this helps, regards and thank you!
egarcia
Limited User
 
Posts: 3
Joined: Tue Jul 28, 2009 7:08 am

Re: Rails erb.html highlighting (ruby embedded on HTML/js)

Postby cody » Wed Jul 29, 2009 11:46 am

Hello,

I don't think that it is going to be that easy. The Ruby and HTML lexers are completely separate beasts and can not be made to work together. You could look at the various keyword slots that each has available and maybe try to manipulate one to do a so so job of highlighting the rails code by using the different keyword slots to fill the need.

I would suggest trying the html lexer and using the html keyword slots for the html, the php keyword slots for the rails tags, and the javascript or python slots for the ruby code. You can see the html.py as an example. This should probably all be done in a separate file (rails.py).

Another option it so implement a completely custom lexer for rails, this is probably the best solution. That is why I pointed you towards issuelist.py and xtext.py as examples of custom lexers. One of Editra's support libraries pygments might already have lexing support for rails in it so that might even make it a little easier.

Will let you know if I think of anything else when I get a chance to look at what rails looks like a little more.

Thanks,

Cody
User avatar
cody
Site Admin
 
Posts: 1315
Joined: Mon Oct 09, 2006 2:49 am
Location: United States

Re: Rails erb.html highlighting (ruby embedded on HTML/js)

Postby dwright15 » Wed Apr 13, 2011 6:51 pm

I would also like .erb file syntax highlighting
Has there been any headway in this area?

(I came across this: http://editra.sourcearchive.com/documen ... ource.html -> Generic `ERB Ruby Templating' lexer) but i'm nor sure what to make of it.
dwright15
Limited User
 
Posts: 1
Joined: Wed Apr 13, 2011 6:43 pm

Re: Rails erb.html highlighting (ruby embedded on HTML/js)

Postby cody » Wed Apr 13, 2011 7:24 pm

Hi,

No, but patches welcomed.

There are some example modules under Editra\src\syntax of using a Pygments lexer to help with styling the code in the text buffer. Also may be able to do it using my suggestion from above to try and use the Scintilla HTML lexer to do the highlighting.


Cody

p.s) never saw that website you linked too but it is pointing to a fairly old version of the code

Tip is always visible in svn: http://svn.wxwidgets.org/viewvc/wx/wxPy ... ty/Editra/
Documentation: http://editra.org/docs/editra_api/namespaces.html
User avatar
cody
Site Admin
 
Posts: 1315
Joined: Mon Oct 09, 2006 2:49 am
Location: United States


Return to Feature Requests

Who is online

Users browsing this forum: No registered users and 1 guest

cron