I integrated the Javascript component SyntaxHighlighter (http://alexgorbatchev.com/SyntaxHighlighter/), I also took a look at MathJax and it's a very good JavaScript component, the only solution to MathML, at least for IE browser unless polluting it with ActiveXs. I actually have no time to try the component and integrate it into AsciiDoc, anyhow I guess it should be threated as the SyntaxHighlighter component so I post here my 'sourcecode' filter/plug-in if anyone interested in both using SyntaxHighlighter and creating a filter for MathJax.

Only few words about AsciiDoc support about it's own documentation, I don't want in any way to blame the author and the contributors, the AsciiDoc documentation is reach and explains in the details how it works, but it doesn't spent much words about the 'language' used in configuration files. I'm not a python programmer, not a LaTeX user, nor a Linux user (FreeBSD user, though), perhaps my point of view is not good. At the end of the story the filter I wrote is a 'I don't know how but it works'. I copied the 'source' filter and modified as needed, there's no support other than 'html' backends (I only tested with custom html5 backend, no idea how it works in xhtml11, but it should works fine too).

How to use the filter
---------------------

I usually use a literal block with attributes, the first attribute is the name of the filter (sourcecode), the second one is the language name as defined by SyntaxHighlighter, the third one specify if gutter (line numbering border) is present and can be 'true' or 'false', it should be optional.

   [sourcecode, language, bool_gutter]
   ------------------------------------------------------
   #include <iostream>
   using namespace std;

   int
   main (int argc, char * argv[])
   {
       for (int i = 0; i < argc; i++)
           cout << argv[i] << endl;
       return 0;
   }
   ------------------------------------------------------

It's not finished, it lacks some features (highlighting lines, toolbar, etc.), also I don't know if it works with callouts.

The filter
----------

Two files, copy them into AsciiDoc filter directory '<ASCIIDOC_ROOT>/filters/sourcecode'

file 'sourcecode-filter.conf'

#
# SyntaxHighlighter filter
#

########################
# Source block templates
########################
[sourcecode-block]
template::[listingblock]

#------
# Code

# HTML backend
ifdef::basebackend-html,basebackend-xhtml11,basebackend-html5[]
[sourcecode-block]
<div class="listingblock sourcecode{role? {role}}">
<a id="{id}"></a>
<div class="title">{caption=}{title}</div>
<div class="content syntaxhighlighter">
<pre class="brush:{language};
gutter:{src_numbered}
">
|
</pre></div></div>
endif::basebackend-html,basebackend-xhtml11,basebackend-html5[]

# DocBook backend
ifdef::basebackend-docbook[]
[sourcecode-block]
<formalpara{id? id="{id}"}{role? role="{role}"}{reftext? xreflabel="{reftext}"}><title>{title}</title><para> {title#}<programlisting language="{language}" linenumbering="{src_numbered=unnumbered}"{args? {args}}> {title%}<programlisting language="{language}"{role? role="{role}"} linenumbering="{src_numbered=unnumbered}"{args? {args}}>
|
</programlisting>
{title#}</para></formalpara>
endif::basebackend-docbook[]

#-------
# Style
ifdef::basebackend-html,basebackend-xhtml11,basebackend-html5[]
[sourcecode-filter-style]
sourcecode-style=template="sourcecode-block",presubs=(),postsubs=("callouts",),posattrs=("style","language","src_numbered","src_tab"),filter="sourcecode-filter.py -backend {basebackend} -tab {tabsize}"
endif::basebackend-html,basebackend-xhtml11,basebackend-html5[]

#########################
# Source paragraph styles
#########################
[paradef-default]
template::[sourcecode-filter-style]

[paradef-literal]
template::[sourcecode-filter-style]

#########################
# Source block styles
#########################
[blockdef-open]
template::[sourcecode-filter-style]

[blockdef-listing]
template::[sourcecode-filter-style]

file 'sourcecode-filter.py'

#!/usr/bin/env python

import os, sys, re, string

VERSION = '1.0.0'

# Options
sOptBackend = ''
bOptHelp = False
sOptLanguage = ''
iOptTab = 4

# Function: ParseCmdLine
def ParseCmdLine (aArgs):
 global sOptBackend, bOptHelp, sOptLanguage, iOptTab
 if (len (aArgs) == 0):
  bOptHelp = True
  return True
 i = 0
 while (i < len (aArgs)):
  sArg = aArgs[i]
  i += 1
  if (sArg == '-backend'):
   sArg = aArgs[i]
   i += 1
   sOptBackend = sArg
  elif (sArg == '-help'):
   bOptHelp = True
  elif (sArg == '-tab'):
   sArg = aArgs[i]
   i += 1
   iOptTab = int (sArg)
  else:
   MsgError ('Invalid command line option \'' + sArg + '\'')
   return False
 return True

# Function: MsgError
def MsgError (sText):
 sys.stderr.write ("\x1B[1;31m" + sText + "\x1B[0m\n")

# Function: ShowHelp
def ShowHelp ():
 print ('Usage:')
 print ('    ' + os.path.basename (sys.argv[0]) + ' [options]')
 print ('')
 print ('Options:')
 print ('    -backend <backend>')
 print ('        The backend to use. Valid backends are:')
 print ('            html')
 print ('            html5')
 print ('            xhtml11')
 print ('            docbook')
 print ('    -help')
 print ('        Show this help.')
 print ('    -tab <tabsize>')
 print ('        The size of the tab, defaults to 4 spaces.')

# Function: Main
def Main ():
 global sOptBackend, bOptHelp, sOptLanguage, iOptTab
 if (not ParseCmdLine (sys.argv[1:])):
  sys.exit (1)
 if (bOptHelp):
  ShowHelp ()
 else:
  if (not Run ()):
   sys.exit (1)
 sys.exit (0)

# Function: Run
def Run ():
 global sOptBackend, bOptHelp, sOptLanguage, iOptTab
 # Checking backend
 if (sOptBackend == ''):
  MsgError ('Backend is not defined')
  return False
elif ((sOptBackend == 'html') or (sOptBackend == 'html5') or (sOptBackend == 'xhtml11')):
  # ???
  i = 0
 elif (sOptBackend == 'docbook'):
  # ???
  i = 0
 else:
  MsgError ('Invalid backend \'' + sOptBackend + '\'')
  return False
 # Checking tab size
 if (iOptTab <= 0):
  MsgError ('Invalid tab size \'' + iOptTab + '\'')
  return False

 # Read stdin for listingblock content, adjust it and write it to stdout
 bLoop = True
 while (bLoop):
  sLine = sys.stdin.readline ()
  if (sLine):
   sLine = string.rstrip (sLine)
   sLine = string.expandtabs (sLine, iOptTab)
   # HTML entities
   sLine = string.replace (sLine, '&', '&amp;')
   sLine = string.replace (sLine, '<', '&lt;')
   sLine = string.replace (sLine, '>', '&gt;')
   sys.stdout.write (sLine + "\n")
  else:
   bLoop = False
 return True

if __name__ == "__main__":
 try:
  Main ()
 except (KeyboardInterrupt, SystemExit):
  pass
 except:
  print_stderr ("%s: unexpected exit status: %s" %
       (os.path.basename (sys.argv[0]), sys.exc_info ()[1]))
 # Exit with previous sys.exit () status or zero if no sys.exit().
 iExitStatus = sys.exc_info ()[1]
 sys.exit (iExitStatus)



--
You received this message because you are subscribed to the Google Groups 
"asciidoc" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/asciidoc.
For more options, visit https://groups.google.com/d/optout.

Reply via email to