Thursday, May 14, 2009

GNU Source-higlight 3.0: now with a Library

I finally made it to release the new major version of GNU Source-highlight 3.0, where the main novelty is that it now includes a C++ library that can be used in other programs to highlight text (not necessarily to generated highlighted text as source-highlight itself does, but for instance, to highlight text in a text editor). This was the first time I implemented a C++ library, so I hope I got it right, mostly at least ;-)

Source-highlight now includes also the program source-highlight-settings, which can be used to check whether source-highlight will be able find its language definition files, and other configuration files, and in case, to store the correct settings in a configuration file, in the user home directory. In particular, the stored configuration file will be called source-highlight.conf and stored in $HOME/.source-highlight/. For the moment, this file only stores the default value for the --data-dir option.

Another new feature is the command line option --regex-range, which, similarly to the option --line-range, allows to specify only a part of the input source to be highlighted (of course, as for --line-range option, environments or states, such as, e.g., comments and strings, are handled correctly even though they cross the range). Ranges can be expressed using regular expressions, with the command line option --regex-range. In this case the beginning of the range will be detected by a line containing (in any point) a string matching the specified regular expression; the end will be detected by a line containing a string matching the same regular expression that started the range. This feature is very useful when we want to document some code (e.g., in this very manual) by showing only specific parts, that are delimited in a ad-hoc way in the source code (e.g., with specific comment patterns).

For instance, consider the following listing:

01: /*
02: This is a classical Hello program
03: to test source-highlight with Java programs.
04:
05: to have an html translation type
06:
07: source-highlight -s java -f html --input Hello.java --output Hello.html
08: source-highlight -s java -f html < Hello.java > Hello.html
09:
10: or type source-highlight --help for the list of options
11:
12: written by
13: Lorenzo Bettini
14: http://www.lorenzobettini.it
15: http://www.gnu.org/software/src-highlite
16: */
17:
18: package hello;
19:
20: import java.io.* ;
21:
22: /**
23: * <p>
24: * A simple Hello World class, used to demonstrate some
25: * features of Java source highlighting.
26: * </p>
27: * TODO: nothing, just to show an highlighted TODO or FIXME
28: *
29: * @author Lorenzo Bettini
30: * @version 2.0
31: */ /// class
32: public class Hello {
33: int foo = 1998 ;
34: int hex_foo = 0xCAFEBABE;
35: boolean b = false;
36: Integer i = null ;
37: char c = '\'', d = 'n', e = '\\' ;
38: String xml = "<tag attr=\"value\">&auml;</tag>", foo2 = "\\" ;
39:
40: /* mymethod */
41: public void mymethod(int i) {
42: // just a foo method
43: }
44: /* mymethod */
45:
46: /* main */
47: public static void main( String args[] ) {
48: // just some greetings ;-) /*
49: System.out.println( "Hello from java2html :-)" ) ;
50: System.out.println( "\tby Lorenzo Bettini" ) ;
51: System.out.println( "\thttp://www.lorenzobettini.it" ) ;
52: if (argc > 0)
53: String param = argc[0];
54: //System.out.println( "bye bye... :-D" ) ; // see you soon
55: }
56: /* main */
57: }
58: /// class
59:
60: // end of file test.java

If we invoke source-highlight with the following command line option:
--regex-range="/// [[:alpha:]]+"
we will get this output (thus, only the part in the range delimited by "/// class", which matches the specified regular expression:

32: public class Hello {
33: int foo = 1998 ;
34: int hex_foo = 0xCAFEBABE;
35: boolean b = false;
36: Integer i = null ;
37: char c = '\'', d = 'n', e = '\\' ;
38: String xml = "<tag attr=\"value\">&auml;</tag>", foo2 = "\\" ;
39:
40: /* mymethod */
41: public void mymethod(int i) {
42: // just a foo method
43: }
44: /* mymethod */
45:
46: /* main */
47: public static void main( String args[] ) {
48: // just some greetings ;-) /*
49: System.out.println( "Hello from java2html :-)" ) ;
50: System.out.println( "\tby Lorenzo Bettini" ) ;
51: System.out.println( "\thttp://www.lorenzobettini.it" ) ;
52: if (argc > 0)
53: String param = argc[0];
54: //System.out.println( "bye bye... :-D" ) ; // see you soon
55: }
56: /* main */
57: }

if we invoke it with
--regex-range="/\* main \*/"
we get the part concerning main:

47:     public static void main( String args[] ) {
48: // just some greetings ;-) /*
49: System.out.println( "Hello from java2html :-)" ) ;
50: System.out.println( "\tby Lorenzo Bettini" ) ;
51: System.out.println( "\thttp://www.lorenzobettini.it" ) ;
52: if (argc > 0)
53: String param = argc[0];
54: //System.out.println( "bye bye... :-D" ) ; // see you soon
55: }

Finally, with
--regex-range="/\* [[:alpha:]]+ \*/"
you'll get only the parts concerning methods:

41:     public void mymethod(int i) {
42: // just a foo method
43: }
47: public static void main( String args[] ) {
48: // just some greetings ;-) /*
49: System.out.println( "Hello from java2html :-)" ) ;
50: System.out.println( "\tby Lorenzo Bettini" ) ;
51: System.out.println( "\thttp://www.lorenzobettini.it" ) ;
52: if (argc > 0)
53: String param = argc[0];
54: //System.out.println( "bye bye... :-D" ) ; // see you soon
55: }

This is quite useful for inserting some parts of input files in manuals (e.g., I used it myself to insert some parts of files in the source-highlight manuals).

Another added functionality is that source-highlight also uses the filename (not only its extension) to detect the source language (for instance, if you pass as input ChangeLog, it will use the changelog.lang language definition).

Finally, two more input languages were added: the one for generic configuration files (including, e.g., xorg.conf, qmake.conf, etc.), and the one for pkg-config files; here are some examples

configuration files

# test file for some configuration files, such as
# xorg, qmake.conf and generic conf files (based on properties)

# xorg.conf (xorg X Window System server configuration file)
#
# This file was generated by dexconf, the Debian X Configuration tool, using
# values from the debconf database.
#

Section "Files"
EndSection

Section "InputDevice"
Identifier "Synaptics Touchpad"
Driver "synaptics"
Option "SendCoreEvents" "true"
Option "Device" "/dev/psaux"
Option "Protocol" "auto-dev"
Option "SHMConfig" "on"
Option "HorizEdgeScroll" "0"
Option "MinSpeed" "0.5" # touchpad speed when moving slowly
Option "MaxSpeed" "2.0" # touchpad speed when moving fast
Option "AccelFactor" "0.10"
Option "CircularScrolling" "on"
Option "EdgeMotionMinSpeed" "0.5"
EndSection

#
# qmake configuration for win32-g++
#
# Written for MinGW
#

MAKEFILE_GENERATOR = MINGW
TEMPLATE = app
CONFIG += qt warn_on release link_prl copy_dir_files debug_and_release debug_and_release_target precompile_header

QMAKE_INCDIR =
QMAKE_INCDIR_QT = $$[QT_INSTALL_HEADERS]
QMAKE_LIBDIR_QT = $$[QT_INSTALL_LIBS]

QMAKE_RUN_CC = $(CC) -c $(CFLAGS) $(INCPATH) -o $obj $src
QMAKE_RUN_CC_IMP = $(CC) -c $(CFLAGS) $(INCPATH) -o $@ $<


# settings for source-highlight

datadir = "/home/bettini/usr/local/share/source-highlight"

# other conf file
[PlaylistColumnsLayout]
ColumnNames=Filename,Title,Artist,AlbumArtist,Composer,Year,Album,DiscNumber,Track,BPM,Genre,Comment,Directory,Type,Length,Bitrate,SampleRate,Score,Rating,PlayCount,LastPlayed,Mood,Filesize
ColumnOrder=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22
ColumnWidths=0,184,92,0,0,100,92,0,0,0,0,0,73,0,80,0,0,0,0,0,0,0,0
ColumnsVersion=1
SortAscending=true
SortColumn=200

pkg-config files

prefix=/home/bettini/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: libsource-highlight
Description: GNU Source-highlight library.
URL: http://www.gnu.org/software/src-highlite/
Version: 3.0
Libs: -L${libdir} -lsource-highlight -L/usr/lib -lboost_regex-gcc43-1_35
Cflags: -I${includedir} -I${libdir}/include -I/usr/include



Hope to have another program using source-highlight library ready soon :-)

No comments: