Changes To Cookbook
Not logged in

Changes to "Cookbook" between 2011-01-28 11:05:34 and 2011-02-05 22:02:35

   468    468   The main purpose of Fossil is to do versioning for source code. Although it provides a standalone server and lets you navigate through the repository files additional features like source code highlighting from my perspective (I am not a developer of Fossil) are out of scope for an SCM. Just keep the Unix principle: small little programs that do their task and do it well.
   469    469   
   470    470   However to have source code highlighted in the presented web pages would still be desirable.
   471    471   
   472    472   <h3>Solution</h3>
   473    473   There are two scenarios how to implement such a feature:
   474    474      *  Fossil pipes the source code through a filter before sending it to the browser. The filter could be defined as a configuration option to Fossil. One solution for this might be the <a href="http://www.gnu.org/software/src-highlite">GNU Source code Highlighting</a> program.
   475         -   *  Use a Javascript library that renders the code within your browser. One solution for this might be <a href="http://code.google.com/p/syntaxhighlighter">Google Syntax Highlighter</a>
          475  +   *  Use a Javascript library that renders the code within your browser. One solution for this might be <a href="http://alexgorbatchev.com/SyntaxHighlighter">Alex Gorbatchev's Syntax Highlighter</a>
   476    476   
   477    477   I estimate that the <i>pipe</i> solution needs some more work/ code changes. Thus I am solely looking at the Javascript solution.
   478    478   
   479         -The SyntaxHighlighter is a library of some Javascript files, a little Flash application and a CSS file. The Flash application is for copying to clipboard, print and view source. You have the option to include all the files into your repository or use the files hosted at Google. The latter may only be an option if you are connected to internet all the time.
          479  +The SyntaxHighlighter is a library of some Javascript files and a CSS file. You have the option to include all the files into your repository, put them on a server you have access to or use the files hosted at alexgorbatchev.com. The latter may only be an option if you are connected to internet all the time.
          480  +To add the syntax highlighting class to the &lt;pre&gt; element, you also need a copy of <a href="http://jquery.com/">jquery</a> in the SyntaxHighlighter directory. 
   480    481   
   481         -For syntax highlighting to work the Header and Footer templates need to be modified and a little code change has to be applied to the Fossil sources.
   482         -The examples below assume you have added the syntax highlighting library to your repository into a directory www/scripts.
          482  +For syntax highlighting to work the Header and Footer templates need to be modified.
          483  +
          484  +The examples below assume you have added the syntax highlighting library to your repository into a directory www/SyntaxHighlighter.
          485  +To make it easier to switch between the scripts beeing part of the repository and the scripts beeing hosted on the internet, the header template defines two TH1 variables:
          486  +  
          487  +  *  highlighterpath - used for all references to the actual location of the SyntaxHighlighter. Just set those variable to the basepath of SyntaxHighlighter and you're done. If you would like to use the hosted version at alexgorbatchev.com, replace <i>$baseurl/doc/tip/www/SyntaxHighlighter</i> with <i>http://alexgorbatchev.com/pub/sh/current</i>
          488  +  *  jquerypath  - used to define the location of the used jquery script. If you want to use a hosted version, replace <i>$baseurl/doc/tip/www/SyntaxHighlighter/jquery-1.5.min.js</i> with  <i>http://code.jquery.com/jquery-1.5.min.js</i> or one of the other URLs availble at the <a href="http://docs.jquery.com/Downloading_jQuery#CDN_Hosted_jQuery">jquery download page</a>.
   483    489   <p>
   484    490   <b>Header</b>
   485    491   <pre>
   486    492   &lt;html&gt;
   487    493    &lt;head&gt;
          494  + &lt;th1&gt;
          495  +   set highlighterpath "$baseurl/doc/tip/www/SyntaxHighlighter"
          496  +   set jquerypath "$baseurl/doc/tip/www/SyntaxHighlighter/jquery-1.5.min.js"
          497  + &lt;/th1&gt; 
   488    498    &lt;title&gt;$&lt;project_name&gt;: $&lt;title&gt;&lt;/title&gt;
   489         - &lt;link rel="alternate" type="application/rss+xml" title="RSS Feed"
   490         -       href="$baseurl/timeline.rss"&gt;
   491         - &lt;link rel="stylesheet" href="$baseurl/style.css" type="text/css"
   492         -       media="screen"&gt;
   493         - &lt;link rel="stylesheet" href="$baseurl/doc/tip/www/SyntaxHighlighter.css" type="text/css"
   494         -       media="screen"&gt;
          499  + &lt;link rel="alternate" type="application/rss+xml" title="RSS Feed" href="$baseurl/timeline.rss"&gt;
          500  + &lt;link rel="stylesheet" href="$baseurl/style.css" type="text/css" media="screen"&gt;
          501  + &lt;link rel="stylesheet" href="$highlighterpath/styles/shCoreDefault.css" type="text/css" media="screen" /&gt;
          502  + &lt;script  language="javascript" src="$jquerypath"&gt;&lt;/script&gt;	   
   495    503    &lt;/head&gt;
   496         -
   497    504   . . .
   498    505   </pre>
   499    506   </p><p>
   500    507   <b>Footer</b>
          508  +
          509  +With version 3.x of SyntaxHighlighter it is possible to load the language specific scripts on demand. The footer template uses this feature to identify the programming language of the sourcefile by looking at the extension of the file and then adds the appropriate brush to the &lt;pre&gt; element. SyntaxHighlighter will then load only the brush needed to highlight the current page.
          510  +
   501    511   <pre>
   502         -&lt;/div&gt;
   503    512   &lt;div class="footer"&gt;
   504    513   Fossil version $manifest_version $manifest_date
   505    514   &lt;/div&gt;
   506         -&lt;script language="javascript" src="$baseurl/doc/tip/www/scripts/shCore.js"&gt;&lt;/script&gt;
   507         -&lt;script language="javascript" src="$baseurl/doc/tip/www/scripts/shBrushCpp.js"&gt;&lt;/script&gt;
          515  +&lt;script language="javascript" src="$highlighterpath/scripts/shCore.js"&gt;&lt;/script&gt;
          516  +&lt;script language="javascript" src="$highlighterpath/scripts/shAutoloader.js">&lt;/script&gt;
   508    517   &lt;script language="javascript"&gt;
   509         -dp.SyntaxHighlighter.ClipboardSwf = '$baseurl/doc/tip/www/scripts/clipboard.swf';
   510         -dp.SyntaxHighlighter.HighlightAll('code');
          518  +function path()
          519  +{
          520  +  var args = arguments, result = &#91;&#93;;       
          521  +  for(var i = 0; i < args.length; i++)
          522  +      result.push(args[i].replace('@', '$highlighterpath/scripts/'));       
          523  +  return result
          524  +};
          525  +// Do the highlighting only on artifact pages
          526  +if (document.getElementsByTagName("title")&#91;0&#93;.innerHTML.indexOf("Artifact") != -1)
          527  +{
          528  +    // try to find out the file type by looking at the file names extension
          529  +    var file = document.getElementsByTagName("blockquote")&#91;0&#93;.getElementsByTagName("a")&#91;0&#93;.innerHTML;
          530  +    var extPos = file.lastIndexOf(".");
          531  +    var extension = "";
          532  +    if (extPos != -1)
          533  +       extension = file.substring(extPos + 1);
          534  +    // set a default for extensions not recognized
          535  +    var brush = "brush: text";
          536  +	// check the extension to select the brush for highlighting
          537  +    if (extension == "cs")
          538  +        brush = "brush: c-sharp";
          539  +    else if (extension == "vb")
          540  +        brush = "brush: vb";
          541  +    else if (extension == "xml" || extension == "xsd" || extension == "xslt" || extension == "aml" || extension == "shfbproj" || extension == "csproj")
          542  +        brush = "brush: xml";
          543  +    else if (extension == "cmd" || extension == "sh")
          544  +        brush = "brush: shell";
          545  +    else if (extension == "sql")
          546  +        brush = "brush: sql";
          547  +    else if (extension == "c" || extension == "cpp" || extension == "cxx" || extension == "cc" || extension == "h" || extension == "hpp")
          548  +        brush = "brush: cpp";
          549  +    else if (extension == "js")
          550  +        brush = "brush: javascript";
          551  +    else if (extension == "css")
          552  +        brush = "brush: css";
          553  +    else if (extension == "php")
          554  +        brush = "brush: php";
          555  +    else if (extension == "pl")
          556  +        brush = "brush: perl";
          557  +    else if (extension == "java")
          558  +        brush = "brush: java";
          559  +    else if (extension == "ruby" || extension == "rb" )
          560  +        brush = "brush: ruby";
          561  +    else if (extension == "py")
          562  +        brush = "brush: python";
          563  +    // disable the highlighter toolbar
          564  +    brush = brush + "; toolbar: false;";
          565  +	// Add the brush to the pre element which contains the source file
          566  +    $("pre:last").addClass(brush);
          567  +	// initialize SyntaxHighlighter's autoloader
          568  +    SyntaxHighlighter.autoloader.apply(null, path(
          569  +		'applescript            @shBrushAppleScript.js',
          570  +		'actionscript3 as3      @shBrushAS3.js',
          571  +		'bash shell             @shBrushBash.js',
          572  +		'coldfusion cf          @shBrushColdFusion.js',
          573  +		'cpp c                  @shBrushCpp.js',
          574  +		'c# c-sharp csharp      @shBrushCSharp.js',
          575  +		'css                    @shBrushCss.js',
          576  +		'delphi pascal          @shBrushDelphi.js',
          577  +		'diff patch pas         @shBrushDiff.js',
          578  +		'erl erlang             @shBrushErlang.js',
          579  +		'groovy                 @shBrushGroovy.js',
          580  +		'java                   @shBrushJava.js',
          581  +		'jfx javafx             @shBrushJavaFX.js',
          582  +		'js jscript javascript  @shBrushJScript.js',
          583  +		'perl pl                @shBrushPerl.js',
          584  +		'php                    @shBrushPhp.js',
          585  +		'text plain             @shBrushPlain.js',
          586  +		'py python              @shBrushPython.js',
          587  +		'ruby rails ror rb      @shBrushRuby.js',
          588  +		'sass scss              @shBrushSass.js',
          589  +		'scala                  @shBrushScala.js',
          590  +		'sql                    @shBrushSql.js',
          591  +		'vb vbnet               @shBrushVb.js',
          592  +		'xml xhtml xslt html    @shBrushXml.js'
          593  +	));
          594  +	// and finally highlight it
          595  +	SyntaxHighlighter.all()
          596  +}
   511    597   &lt;/script&gt;
   512    598   &lt;/body&gt;&lt;/html&gt;
   513    599   </pre>
   514         -</p><p>
   515         -<b>Fossil/src/info.c</b> function artifact_page
   516         -<pre>
   517         -if( zMime==0 ){
   518         -    @ &lt;pre name="code" class="c"&gt;
   519         -    @ %h(blob_str(&content))
   520         -    @ &lt;/pre&gt;
   521         -</pre>
   522    600   </p>
   523         -<p>
   524         -If you only expect one language to be highlighted on a page, then you can implement the above without changing the Fossil code. Just use JQuery in the header like this (this example is for Visual Basic):
   525         -<pre>
   526         -<script src="http://code.jquery.com/jquery-1.4.4.js"></script>
   527         -</pre>
   528         -and in the footer like this:
   529         -<pre>
   530         -<script>$("pre:last").addClass("brush: vb");</script>
   531         -<script>
   532         -$(document).ready(function() {SyntaxHighlighter.all()});     
   533         -</script>
   534         -</pre>
   535         -</p>
   536         -<h3>Discussion</h3>
   537         -The Javascript solution requires a minimum to be fully supported by Fossil. Of course my litte change only applies for C/C++ files. But only little more work needs to be done to get the extension of the file a guess the file type.
   538         -
   539         -The pipe solution would also be nice but would probably need some more work than this little patch.
   540         -
   541    601   <h2><a name="win32dev">How to prepare your Windows XP Fossil development Environment</a></h2>
   542    602   <h4>MinGW+NSIS/WiX</h4>
   543    603   <ul>
   544    604   <li>Setup TCL</li>
   545    605   <li>Setup MinGW</li>
   546    606   <li>Getting NSIS Windows packaging tool </li>
   547    607   <li>Getting Fossil source code</li>