N3/Turtle Highlighting

In working with semantic web triples,  Turtle is much easier to work with than rdf/xml. The lack of syntax highlighting for N3/Turtle has been a problem. I didn't find any good solutions so I offer two tools up for public consumption.

Both follow my belief that highlighting should be based on the part of the statement. Subjects one color, predicates another and objects a different color (actually a couple of colors for literal vs resource objects). Coloring is to provide visual clues to the semantic web worker. A given qname could be a subject, predicate or object and should be colored accordingly. Not colored one way in all three cases and colored differently if the uri is put in in the place of the qname. That is the approach that both the following tools use.

Komodo

 Komodo is an opensource editor that I've done some syntax highlighting for. komodo-logo.gif

I have only released this early version as I start to consider a more full-fledged move from Textmate (see below) to Komodo. Both editors are eminently

editable but Komodo with it's Mozilla routes may have more potential as a Semantic Web platform.

Installation

This is released as a Komodo extension and is installed the same way as any other extension. It can be found at the  ActiveState Extensions site.

TextMate

I have created a bundle for use with the  TextMate editor. textmate_64.pngIf you edit  N3 or  Turtle files on OSX, check it out. If you are not on OSX...move along, there is nothing to see here.

To install whatever the current trunk, go to your TextMate bundles dir and use:

svn co http://www.openvest.com/svn/public/tools/trunk/N3.tmbundle/

Setup

The installation might leave you unimpressed.

The Bundle does not reuse all pre-existing scopes and keywords. I just created subject, predicate and object scopes and gave them colors. You might see all black unless you create them in the preferences menu and the fonts and colors tab under that.

Anyway for semantic web stuff, I thought the subject, predicate object scopes made a lot of sense.

In my Mac Classic theme I have:

Element Name Scope Style
rdf:statement meta.statement
rdf:subject meta.statement.subject bold maroon
rdf:predicate meta.statement.predicate italic darkgreen
rdf:object meta.statement.object midnightblue

If you do that, you should start with N3 like:

joseki:ProcessorSPARQL
    rdfs:label "General SPARQL processor" ;
    rdf:type joseki:Processor ;
    module:implementation joseki:ImplSPARQL ;

    # Parameters - this processor processes FROM/FROM NAMED
    joseki:allowExplicitDataset       "true"^^xsd:boolean ;
    joseki:allowWebLoading            "true"^^xsd:boolean ;
    .

joseki:ProcessorSPARQL_FixedDS
    rdfs:label "SPARQL processor for fixed datasets" ;
    rdf:type joseki:Processor ;
    module:implementation joseki:ImplSPARQL ;

    # This processor does not accept queries with FROM/FROM NAMED
    joseki:allowExplicitDataset       "false"^^xsd:boolean ;
    joseki:allowWebLoading            "false"^^xsd:boolean ;

And see it as something like:

TextMate N3 with highliting

SPARQL

I've started to work on some basic  SPARQL markup. Check it out in trac(pygments based):

# Sample SPARQL of XBRL data
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix usgaap: <http://xbrl.us/us-gaap/2008-01-31>
prefix xbrli: <http://www.xbrl.org/2003/instance>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>


SELECT ?cash ?date  
WHERE{
[]  rdf:type usgaap:CashAndCashEquivalentsAtCarryingValue ;
    rdf:value ?cash;
    xbrli:context [xbrli:entity [xbrli:identifier [xbrli:schema <http://www.nyse.com/>;
                                                   rdf:value  "IBM"];
                   xbrli:period [xbrli:onDate ?date ]]].
FILTER (?date > ?some "2005-12-31"^^xsd:date)                    
}

Can also check it out in restructured text  here(also pygments).

If you use TextMate, check out that lexer also.

In TextMate, a macro that would take SPARQL and executed it against a SPARQL endpoint (like a  Joseki server) might be cool but I think I'd rather put that effort into Javascript for now.

Pygments

 Pygments is a fast, flexible  python based syntax highlighter. pygments_75.pngIt is available for use in Trac and will be the default highlighter in release 0.11 .

SPARQL highlighting has not yet been implemented here!

Installation

1. Install  Pygments if you have not done so.

2. Grab sw.py from the source browser or from the svn with:

#From your pygments subdir like: 
# path-to-your/python2.x/site-packages/Pygments-0.x-py2.x.egg/pygments
#execute
svn co  http://www.openvest.com/svn/public/tools/trunk/pygments/lexers

make sure do to that from within your /some_path/some_dir/site-packages/??/pygments/lexers directory.

3. To rebuild the lexers list, in that same subdir run:

python _mapping.py

You should then be able to pygmentize an n3 file.

Trac Integration

Once pygments is setup for !n3SyntaxHighlighting you only need to begin n3 code like:

{{{
#!n3
joseki:ProcessorSPARQL ... 
}}}

Within Trac you should be able to make n3 files look like:

joseki:ProcessorSPARQL
    rdfs:label "General SPARQL processor" ;
    rdf:type joseki:Processor ;
    module:implementation joseki:ImplSPARQL ;

    # Parameters - this processor processes FROM/FROM NAMED
    joseki:allowExplicitDataset       "true"^^xsd:boolean ;
    joseki:allowWebLoading            "true"^^xsd:boolean ;
    .

joseki:ProcessorSPARQL_FixedDS
    rdfs:label "SPARQL processor for fixed datasets" ;
    rdf:type joseki:Processor ;
    module:implementation joseki:ImplSPARQL ;

Instead of:

joseki:ProcessorSPARQL
    rdfs:label "General SPARQL processor" ;
    rdf:type joseki:Processor ;
    module:implementation joseki:ImplSPARQL ;

    # Parameters - this processor processes FROM/FROM NAMED
    joseki:allowExplicitDataset       "true"^^xsd:boolean ;
    joseki:allowWebLoading            "true"^^xsd:boolean ;
    .

joseki:ProcessorSPARQL_FixedDS
    rdfs:label "SPARQL processor for fixed datasets" ;
    rdf:type joseki:Processor ;
    module:implementation joseki:ImplSPARQL ;

The exact coloring is under your control as Pygments has numerous color schemes.

You can go to the SandBox and try it out. If there are highlighting errors please email me or create a New Ticket at the link above.

Trac 0.10.x will require that you download and install the  TracPygmentsPlugin.

Trac 0.11 already uses Pygments but you may have to modify your trac.ini file to include:

[mimeviewer]
mime_map = ...(some-stuff)...,application/x-turtle:n3 
pygments_default_style = trac
pygments_modes = application/x-turtle:n3:7

reStructuredText Integration

Code blocks in  reStructuredText will also now be marked up for N3 code blocks. Just follow the  pygments/restructuredText integration instructions. You can then have markup in Moin or where ever you use reStructuredText .

Other Highlighting

Emacs

Emacs has an  N3 Emacs mode which I found disappointing to say the least. I am a long time emacs user but the lack of a good tool here is what motivated me to create the TextMate bundle.

vim

VI has an  N3 syntax script available. I can't comment because I have never been a vim kind of guy. Vim users can also use the  RDF Namespace completion script. (Maybe I should implement this for TextMate)

Attachments