Permalink
Browse files

Documentation updates, add shell script examples to README

  • Loading branch information...
1 parent 8c4adfb commit c5e5a63904a07a62e758e06f901408875a9121ae @xolox committed Oct 4, 2011
Showing with 107 additions and 9 deletions.
  1. +50 −3 README.md
  2. +2 −1 TODO.md
  3. +4 −1 etc/demo.lua
  4. +2 −2 etc/lxsh
  5. +43 −0 examples/gvim.sh
  6. +3 −1 src/lexers/bib.lua
  7. +3 −1 src/lexers/sh.lua
View
@@ -1,6 +1,6 @@
# LXSH: Lexing & Syntax Highlighting in Lua
-LXSH is a collection of [lexers] [lexing] and [syntax highlighters] [highlighting] written in [Lua] [lua] using the excellent pattern-matching library [LPeg] [lpeg]. Two programming languages are currently supported, these are Lua and C. The syntax highlighters support three output formats: [HTML] [html] designed to be easily embedded in web pages, [LaTeX] [latex] which can be used to generate high quality PDF files and [RTF] [rtf] which can be used in graphical text editors like Microsoft Word and LibreOffice (formerly OpenOffice). Three predefined color schemes are included. Here are some examples of the supported input languages, output formats and color schemes:
+LXSH is a collection of [lexers] [lexing] and [syntax highlighters] [highlighting] written in [Lua] [lua] using the excellent pattern-matching library [LPeg] [lpeg]. Several syntaxes are currently supported: Lua, C, BibTeX and shell script. The syntax highlighters support three output formats: [HTML] [html] designed to be easily embedded in web pages, [LaTeX] [latex] which can be used to generate high quality PDF files and [RTF] [rtf] which can be used in graphical text editors like Microsoft Word and LibreOffice (formerly OpenOffice). Three predefined color schemes are included. Here are some examples of the supported input languages, output formats and color schemes:
<table cellspacing=0 cellpadding=4>
<tr>
@@ -81,6 +81,30 @@ LXSH is a collection of [lexers] [lexing] and [syntax highlighters] [highlightin
<a href="http://peterodding.com/code/lua/lxsh/examples/wiki/lua_apr.c.rtf">RTF</a>
</td>
</tr>
+ <tr>
+ <th style="border-right:1px solid silver; text-align: right">Shell script:</th>
+ <td style="border-right: 1px solid silver; border-bottom: 1px solid silver">
+ <a href="http://peterodding.com/code/lua/lxsh/examples/earendel/gvim.sh.html">HTML</a>
+ <span style="color: silver">·</span>
+ <a href="http://peterodding.com/code/lua/lxsh/examples/earendel/gvim.sh.pdf">PDF</a>
+ <span style="color: silver">·</span>
+ <a href="http://peterodding.com/code/lua/lxsh/examples/earendel/gvim.sh.rtf">RTF</a>
+ </td>
+ <td style="border-right: 1px solid silver; border-bottom: 1px solid silver">
+ <a href="http://peterodding.com/code/lua/lxsh/examples/slate/gvim.sh.html">HTML</a>
+ <span style="color: silver">·</span>
+ <a href="http://peterodding.com/code/lua/lxsh/examples/slate/gvim.sh.pdf">PDF</a>
+ <span style="color: silver">·</span>
+ <a href="http://peterodding.com/code/lua/lxsh/examples/slate/gvim.sh.rtf">RTF</a>
+ </td>
+ <td style="border-right: 1px solid silver; border-bottom: 1px solid silver">
+ <a href="http://peterodding.com/code/lua/lxsh/examples/wiki/gvim.sh.html">HTML</a>
+ <span style="color: silver">·</span>
+ <a href="http://peterodding.com/code/lua/lxsh/examples/wiki/gvim.sh.pdf">PDF</a>
+ <span style="color: silver">·</span>
+ <a href="http://peterodding.com/code/lua/lxsh/examples/wiki/gvim.sh.rtf">RTF</a>
+ </td>
+ </tr>
</table>
As you may have noticed in the above examples, the syntax highlighters replace standard library identifiers (and then some) with hyperlinks to the relevant documentation. You can also try switching between style sheets while staying on the same web page by using your web browser's *View → Page styles* menu (this works using so-called "alternate style sheets").
@@ -102,7 +126,7 @@ If you don't have LuaRocks installed you can [download the latest release] [zipb
## Usage
-If you want to call a lexer or access an LPeg pattern defined by a lexer you can do so as follows (this example demonstrates the Lua lexer but the C lexer works the same):
+If you want to call a lexer or access an LPeg pattern defined by a lexer you can do so as follows (this example demonstrates the Lua lexer but the other lexers work the same way):
> -- Load the LXSH module.
> require 'lxsh'
@@ -126,7 +150,7 @@ If you want to call a lexer or access an LPeg pattern defined by a lexer you can
> -- Use one of the patterns defined by the lexer.
> lxsh.lexers.lua.patterns.comment:match '--[=[ this is a long comment ]=]'
-Lexers define the following functions:
+Note that you only need to load the main LXSH module with `require()`, the lexer and highlighter submodules are automatically loaded as they're first used. Lexers define the following functions:
* `lexer.find(subject [, init [, options ]])` takes a string and optional starting position, matches a single token (anchored) and returns two values: the token kind and the last matched character
* `lexer.match(subject [, init [, options ]])` takes a string and optional starting position, matches a single token (anchored) and returns two values: the token kind and the matched text
@@ -178,6 +202,29 @@ The C lexer produces the following tokens:
* preprocessor
* whitespace
+The BibTeX lexer produces the following tokens:
+
+ * entry (e.g. `@Book`)
+ * field (e.g. `author`)
+ * identifier
+ * string
+ * number
+ * operator
+ * delimiter
+ * whitespace
+ * error (invalid input)
+
+The shell script lexer produces the following tokens:
+
+ * comment
+ * number
+ * string
+ * variable
+ * operator
+ * keyword
+ * command
+ * error (invalid input)
+
## Contact
If you have questions, bug reports, suggestions, etc. the author can be contacted at <peter@peterodding.com>. The latest version is available at <http://peterodding.com/code/lua/lxsh/> and <http://github.com/xolox/lua-lxsh>.
View
@@ -1,4 +1,5 @@
# To-do list for LXSH
* Proper support for Lua's interactive mode! (this is something I miss in every other highlighter out there)
- * Support for lexing and highlighting of shell scripts, SGML (HTML and XML), CSS
+ * Support for lexing and highlighting of SGML (HTML and XML), CSS
+ * Make the documentation links optional (but enabled by default)
View
@@ -31,8 +31,9 @@ for _, formatter in ipairs { lxsh.formatters.html, lxsh.formatters.rtf, lxsh.for
io.stderr:write(('Wrote %iK to %s!\n'):format(nbytes/1024, outfile))
if outfile:find '%.tex$' then
-- Try to run the LaTeX PDF compiler (tested on Ubuntu 10.04 with texlive).
+ -- Requires "Bera Mono" (try `sudo apt-get install texlive-fonts-extra').
local outdir, filename = outfile:match '^(.-)([^/]*)$'
- local command = 'pdflatex -interaction errorstopmode -halt-on-error ' .. filename .. ' >/dev/null 2>&1'
+ local command = 'pdflatex -interaction batchmode -halt-on-error ' .. filename -- .. ' >/dev/null 2>&1'
io.stderr:write(" - Compiling ", outfile:gsub('%.tex$', '.pdf'), ": ")
local status = os.execute('cd ' .. outdir .. ' && ' .. command .. ' && ' .. command)
io.stderr:write(status == 0 and "OK" or "Failed! (do you have LaTeX installed?)", "\n")
@@ -49,5 +50,7 @@ for _, formatter in ipairs { lxsh.formatters.html, lxsh.formatters.rtf, lxsh.for
demo('examples/prompt.lua', 'examples/' .. colors .. '/prompt.lua' .. formatter.extension, lxsh.highlighters.lua)
-- Highlight example C source code (also from my Lua/APR binding).
demo('examples/lua_apr.c', 'examples/' .. colors .. '/lua_apr.c' .. formatter.extension, lxsh.highlighters.c)
+ -- Highlight example shell script code (something random from my ~/bin).
+ demo('examples/gvim.sh', 'examples/' .. colors .. '/gvim.sh' .. formatter.extension, lxsh.highlighters.sh)
end
end
View
@@ -5,7 +5,7 @@
Command line interface for the LXSH syntax highlighters.
Author: Peter Odding <peter@peterodding.com>
- Last Change: September 29, 2011
+ Last Change: October 4, 2011
URL: http://peterodding.com/code/lua/lxsh/
]]
@@ -20,7 +20,7 @@ function main()
-- Find the syntax highlighter for the input file's type.
local intype = fnamext(infile)
local highlighter = assert(lxsh.highlighters[intype],
- string.format("Invalid input format '%s'! (expected one of 'c', 'lua', 'bib')", intype))
+ string.format("Invalid input format '%s'! (expected one of 'c', 'lua', 'bib', 'sh')", intype))
-- Find the formatter for the output file's type.
local outtype = fnamext(outfile)
local formatter = assert(lxsh.formatters[outtype],
View
@@ -0,0 +1,43 @@
+#!/bin/bash
+
+# This UNIX shell script makes graphical Vim use one instance where possible.
+# Save this script as ~/bin/gvim, make sure it's executable and you're off!
+
+# Configuration variables.
+GVIM=/usr/local/bin/gvim
+WMCTRL=/usr/bin/wmctrl # <- try `sudo apt-get install wmctrl'
+DEBUG=no # yes or no
+
+# Look for command-line arguments that aren't filenames and
+# if one is found then fall back to Vim's regular behavior.
+for ARG in "$@"; do
+ if echo "$ARG" | grep -q '^note:'; then
+ # Don't consider "note:..." as non-file argument (this is a
+ # hack to make my note taking plug-in work with this script).
+ continue
+ elif [ ! -f "$ARG" ]; then
+ # Replace the wrapper with Vim itself.
+ [ $DEBUG = yes ] && echo "Process $$: Executing $GVIM $@" >&2
+ exec $GVIM "$@"
+ exit # <- shouldn't be reached!
+ fi
+done
+
+# Check if Vim is already running.
+if ! pidof $GVIM > /dev/null; then
+ # It's not: Open file(s) in new instance.
+ [ $DEBUG = yes ] && "Process $$: Executing $GVIM -p $@" >&2
+ exec "$GVIM" -p "$@"
+else
+ [ $DEBUG = yes ] && echo "Process $$: Requesting server list" >&2
+ $GVIM --serverlist | grep '^GVIM[0-9]*$' | while read VIM_SERVER_NAME; do
+ [ $DEBUG = yes ] && echo "Process $$: Raising selected Vim server" >&2
+ $WMCTRL -xa $VIM_SERVER_NAME
+ # Pass any pathname arguments on to Vim?
+ if [ $# -ge 1 ]; then
+ # Open files on command-line in existing Vim window.
+ [ $DEBUG = yes ] && echo "Process $$: Executing $GVIM --servername '$VIM_SERVER_NAME' --remote-tab-silent $@" >&2
+ exec $GVIM --servername "$VIM_SERVER_NAME" --remote-tab-silent "$@"
+ fi
+ done
+fi
View
@@ -5,9 +5,11 @@
Authors:
- Brendan O'Flaherty
- Peter Odding
- Last Change: September 30, 2011
+ Last Change: October 4, 2011
URL: http://peterodding.com/code/lua/lxsh/
+ TODO Comments?!
+
]]
local lxsh = require 'lxsh'
View
@@ -3,9 +3,11 @@
Lexer for shell script code powered by LPeg.
Author: Peter Odding <peter@peterodding.com>
- Last Change: September 30, 2011
+ Last Change: October 4, 2011
URL: http://peterodding.com/code/lua/lxsh/
+ TODO Whitespace? :-P
+
]]
local lxsh = require 'lxsh'

0 comments on commit c5e5a63

Please sign in to comment.