+ Reply to Thread
Results 1 to 10 of 10

Thread: Custom Syntax Highlighter

  1. #1
    Join Date
    May 2009
    Location
    Houston, Tx
    Posts
    237

    Default Custom Syntax Highlighter

    FORTRAN is used a lot at the company that I work for. So when I found a way to extend the SyntaxHighlighter, I was excited. But I can't seem to figure out how to add the syntax to my wiki in such a way that it works with the already-working SyntaxHighlighter.
    I wrote an extension to add to the syntax name space:
    Code:
    <extension>
      <title>Blake Harms Syntax Extension Extension</title>
      <label>Syntax Fortran</label>
      <copyright></copyright>
      <description>This extension contains functions for highlighting syntax of FORTRAN.</description>
      <uri.help></uri.help>
      <namespace>syntax</namespace>
      <config>
        <param name="resources-uri" type="uri">Base uri for syntax service javascript files (default: http://scripts.mindtouch.com/res/syntax/)</param>
      </config>
      <function transform="pre">
        <name>Fortran</name>
        <description>Highlight Fortran syntax</description>
        <param name="code" type="str">source code to highlight</param>
        <param name="collapse" type="bool" optional="true">collapse code view (default: false)</param>
        <param name="firstline" type="num" optional="true">first line number (default: 1)</param>
        <return>
          <html xmlns:eval="http://mindtouch.com/2007/dekiscript">
            <head block="var baseUri = config['resources-uri'] ?? 'http://scripts.mindtouch.com/res/syntax'">
                  <link type="text/css" rel="stylesheet" eval:href="uri.appendpath(baseUri, 'SyntaxHighlighter.css')" />
                  <script type="text/javascript" eval:src="uri.appendpath(baseUri, 'shCore.js')" />
                  <!--<script type="text/javascript" eval:src="uri.appendpath(baseUri, 'shBrushPowershell.js')" /> -->
                  <script type="text/javascript" src="http://wiki/@api/deki/files/1533/=Syntax.Fortran.js" />
            </head>
            <body>
              <pre name="code" eval:class="'fortran' .. (args.collapse == 'true' ? ':collapse' : '') .. (args.firstline ? ':firstline[' .. args.firstline .. ']' : '');"><eval:expr value="args.code"/></pre>
            </body>
            <tail>
              <script type="text/javascript">dp.SyntaxHighlighter.HighlightAll('code');</script>
            </tail>
          </html>
        </return>
      </function>
    </extension>
    And I added a custom brush:
    Code:
    dp.sh.Brushes.Fortran = function()
    {
        var funcs         = 'abs achar acos acosd adjustl adjustr aimag aimax0 aimin0 aint ajmax0 ajmin0 akmax0 akmin0 all allocated alog alog10 amax0 amax1 amin0 amin1 amod anint any asin asind associated atan atan2 atan2d atand bitest bitl bitlr bitrl bjtest bit_size bktest break btest cabs ccos cdabs cdcos cdexp cdlog cdsin cdsqrt ceiling cexp char clog cmplx conjg cos cosd cosh count cpu_time cshift csin csqrt dabs dacos dacosd dasin dasind datan datan2 datan2d datand date date_and_time dble dcmplx dconjg dcos dcosd dcosh dcotan ddim dexp dfloat dflotk dfloti dflotj digits dim dimag dint dlog dlog10 dmax1 dmin1 dmod dnint dot_product dprod dreal dsign dsin dsind dsinh dsqrt dtan dtand dtanh eoshift epsilon errsns exp exponent float floati floatj floatk floor fraction free huge iabs iachar iand ibclr ibits ibset ichar idate idim idint idnint ieor ifix iiabs iiand iibclr iibits iibset iidim iidint iidnnt iieor iifix iint iior iiqint iiqnnt iishft iishftc iisign ilen imax0 imax1 imin0 imin1 imod index inint inot int int1 int2 int4 int8 iqint iqnint ior ishft ishftc isign isnan izext jiand jibclr jibits jibset jidim jidint jidnnt jieor jifix jint jior jiqint jiqnnt jishft jishftc jisign jmax0 jmax1 jmin0 jmin1 jmod jnint jnot jzext kiabs kiand kibclr kibits kibset kidim kidint kidnnt kieor kifix kind kint kior kishft kishftc kisign kmax0 kmax1 kmin0 kmin1 kmod knint knot kzext lbound leadz len len_trim lenlge lge lgt lle llt log log10 logical lshift malloc matmul max max0 max1 maxexponent maxloc maxval merge min min0 min1 minexponent minloc minval mod modulo mvbits nearest nint not nworkers number_of_processors pack popcnt poppar precision present product radix random random_number random_seed range real repeat reshape rrspacing rshift scale scan secnds selected_int_kind selected_real_kind set_exponent shape sign sin sind sinh size sizeof sngl snglq spacing spread sqrt sum system_clock tan tand tanh tiny transfer transpose trim ubound unpack verify';
        var keywords    = 'access action advance allocatable allocate apostrophe assign assignment associate asynchronous backspace bind blank blockdata call case character class close common complex contains continue cycle data deallocate decimal delim default dimension direct do dowhile double doubleprecision else elseif elsewhere encoding end endassociate endblockdata enddo endfile endforall endfunction endif endinterface endmodule endprogram endselect endsubroutine endtype endwhere entry eor equivalence err errmsg exist exit external file flush fmt forall form format formatted function go goto id if implicit in include inout integer inquire intent interface intrinsic iomsg iolength iostat kind len logical module name named namelist nextrec nml none nullify number only open opened operator optional out pad parameter pass pause pending pointer pos position precision print private program protected public quote read readwrite real rec recl recursive result return rewind save select selectcase selecttype sequential sign size stat status stop stream subroutine target then to type unformatted unit use value volatile wait where while write';
    
        this.regexList = [
            { regex: new RegExp("C (.*)$","g"),                            css: 'comments' },
            { regex: dp.sh.RegexLib.singleQuotedString,                        css: 'string' },
            { regex: new RegExp(this.GetKeywords(funcs), 'gmi'),                css: 'color2' },
            { regex: new RegExp(this.GetKeywords(keywords), 'gmi'),                css: 'keyword' }
            ];
    };
    dp.sh.Brushes.Fortran.prototype = new dp.sh.Highlighter();
    dp.sh.Brushes.Fortran.Aliases=['fortran'];
    But I can't seem to get it to work...
    I looked in the error console and found this:
    P.S. - I've never actually worked with FORTRAN, but nearly everyone else here has...
    P.S.S - FORTRAN is a very angry language.
    P.S.S.S - If this works and anyone wants it, its yours (that means YOU Mindtouch team...).

  2. #2
    Join Date
    May 2009
    Location
    Houston, Tx
    Posts
    237

    Default

    Ah HAH!
    I fixed it.
    Here you go:
    Code:
    dp.sh.Brushes.Fortran = function()
    {
        var funcs         = 'abs achar acos acosd adjustl adjustr aimag aimax0 aimin0 aint ajmax0 ajmin0 akmax0 akmin0 all allocated alog alog10 amax0 amax1 amin0 amin1 amod anint any asin asind associated atan atan2 atan2d atand bitest bitl bitlr bitrl bjtest bit_size bktest break btest cabs ccos cdabs cdcos cdexp cdlog cdsin cdsqrt ceiling cexp char clog cmplx conjg cos cosd cosh count cpu_time cshift csin csqrt dabs dacos dacosd dasin dasind datan datan2 datan2d datand date date_and_time dble dcmplx dconjg dcos dcosd dcosh dcotan ddim dexp dfloat dflotk dfloti dflotj digits dim dimag dint dlog dlog10 dmax1 dmin1 dmod dnint dot_product dprod dreal dsign dsin dsind dsinh dsqrt dtan dtand dtanh eoshift epsilon errsns exp exponent float floati floatj floatk floor fraction free huge iabs iachar iand ibclr ibits ibset ichar idate idim idint idnint ieor ifix iiabs iiand iibclr iibits iibset iidim iidint iidnnt iieor iifix iint iior iiqint iiqnnt iishft iishftc iisign ilen imax0 imax1 imin0 imin1 imod index inint inot int int1 int2 int4 int8 iqint iqnint ior ishft ishftc isign isnan izext jiand jibclr jibits jibset jidim jidint jidnnt jieor jifix jint jior jiqint jiqnnt jishft jishftc jisign jmax0 jmax1 jmin0 jmin1 jmod jnint jnot jzext kiabs kiand kibclr kibits kibset kidim kidint kidnnt kieor kifix kind kint kior kishft kishftc kisign kmax0 kmax1 kmin0 kmin1 kmod knint knot kzext lbound leadz len len_trim lenlge lge lgt lle llt log log10 logical lshift malloc matmul max max0 max1 maxexponent maxloc maxval merge min min0 min1 minexponent minloc minval mod modulo mvbits nearest nint not nworkers number_of_processors pack popcnt poppar precision present product radix random random_number random_seed range real repeat reshape rrspacing rshift scale scan secnds selected_int_kind selected_real_kind set_exponent shape sign sin sind sinh size sizeof sngl snglq spacing spread sqrt sum system_clock tan tand tanh tiny transfer transpose trim ubound unpack verify';
        var keywords    = 'access action advance allocatable allocate apostrophe assign assignment associate asynchronous backspace bind blank blockdata call case character class close common complex contains continue cycle data deallocate decimal delim default dimension direct do dowhile double doubleprecision else elseif elsewhere encoding end endassociate endblockdata enddo endfile endforall endfunction endif endinterface endmodule endprogram endselect endsubroutine endtype endwhere entry eor equivalence err errmsg exist exit external file flush fmt forall form format formatted function go goto id if implicit in include inout integer inquire intent interface intrinsic iomsg iolength iostat kind len logical module name named namelist nextrec nml none nullify number only open opened operator optional out pad parameter pass pause pending pointer pos position precision print private program protected public quote read readwrite real rec recl recursive result return rewind save select selectcase selecttype sequential sign size stat status stop stream subroutine target then to type unformatted unit use value volatile wait where while write';
    
        this.regexList = [
            { regex: new RegExp("C (.*)$","gmi"),                            css: 'comment' },
            { regex: dp.sh.RegexLib.SingleQuotedString,                        css: 'string' },
            { regex: new RegExp(this.GetKeywords(funcs), 'gmi'),                css: 'color2' },
            { regex: new RegExp(this.GetKeywords(keywords), 'gmi'),                css: 'keyword' }
            ];    
            
            this.CssClass='dp-fortran';
    };
    dp.sh.Brushes.Fortran.prototype = new dp.sh.Highlighter();
    dp.sh.Brushes.Fortran.Aliases=['fortran'];
    FORTRAN

    Just fyi - this uses the standard FORTRAN comments. I.E. -
    Code:
     C THIS IS A COMMENT
    As opposed to
    Code:
     // This is a comment or
    /* this is a comment or */
    # this is a comment or
    ! this is a comment...

  3. #3
    Join Date
    May 2009
    Location
    Houston, Tx
    Posts
    237

    Default

    Getting a
    error on line 35 in Firefox and only firefox.
    Anyone have any clue?

  4. #4
    Join Date
    Jul 2006
    Location
    San Diego, CA
    Posts
    5,450

    Default

    Maybe there is a double inclusion. Is that possible?

    BTW, did you try out the Syntax Highlighter 2.0? I find the presentation of the syntax much cleaner. However, it requires MindTouch 9.02 or later
    Steve G. Bjorg - Chief Architect
    Did you check the MindTouch FAQ?
    Found a bug? Report it.
    Follow me on Twitter
    Find us on IRC: irc.freenode.net #mindtouch

  5. #5
    Join Date
    May 2009
    Location
    Houston, Tx
    Posts
    237

    Default

    I just popped in the manifest url. I do like the new look. "double inclusion"? As in 1 keyword is in there twice? Maybe. I didn't look at the keywords at all as they mean nothing to me. :P
    I did find and delete the keyword "Precision" in the var funcs because it was also in keywords and that fixed it. But I don't know what that will mean for FORTRAN. Is precision a function or a keyword? Can it be both?

  6. #6
    Join Date
    Jul 2006
    Location
    San Diego, CA
    Posts
    5,450

    Default

    By "double inclusion" I meant a JS file that gets included twice and corrupts itself in doing so.

    I think a word can be both a function and keyword. It will probably just match the first one it finds.
    Steve G. Bjorg - Chief Architect
    Did you check the MindTouch FAQ?
    Found a bug? Report it.
    Follow me on Twitter
    Find us on IRC: irc.freenode.net #mindtouch

  7. #7
    Join Date
    May 2009
    Location
    Houston, Tx
    Posts
    237

    Default

    It didn't. Somehow, it matches both. I figured this out when I realized that the term precision was showing up twice after being transformed to the syntax highlighter.

  8. #8

    Default

    Quote Originally Posted by blakeh View Post
    Ah HAH!
    I fixed it.
    Here you go:
    Code:
    dp.sh.Brushes.Fortran = function()
    {
        var funcs         = 'abs achar acos acosd adjustl adjustr aimag aimax0 aimin0 aint ajmax0 ajmin0 akmax0 akmin0 all allocated alog alog10 amax0 amax1 amin0 amin1 amod anint any asin asind associated atan atan2 atan2d atand bitest bitl bitlr bitrl bjtest bit_size bktest break btest cabs ccos cdabs cdcos cdexp cdlog cdsin cdsqrt ceiling cexp char clog cmplx conjg cos cosd cosh count cpu_time cshift csin csqrt dabs dacos dacosd dasin dasind datan datan2 datan2d datand date date_and_time dble dcmplx dconjg dcos dcosd dcosh dcotan ddim dexp dfloat dflotk dfloti dflotj digits dim dimag dint dlog dlog10 dmax1 dmin1 dmod dnint dot_product dprod dreal dsign dsin dsind dsinh dsqrt dtan dtand dtanh eoshift epsilon errsns exp exponent float floati floatj floatk floor fraction free huge iabs iachar iand ibclr ibits ibset ichar idate idim idint idnint ieor ifix iiabs iiand iibclr iibits iibset iidim iidint iidnnt iieor iifix iint iior iiqint iiqnnt iishft iishftc iisign ilen imax0 imax1 imin0 imin1 imod index inint inot int int1 int2 int4 int8 iqint iqnint ior ishft ishftc isign isnan izext jiand jibclr jibits jibset jidim jidint jidnnt jieor jifix jint jior jiqint jiqnnt jishft jishftc jisign jmax0 jmax1 jmin0 jmin1 jmod jnint jnot jzext kiabs kiand kibclr kibits kibset kidim kidint kidnnt kieor kifix kind kint kior kishft kishftc kisign kmax0 kmax1 kmin0 kmin1 kmod knint knot kzext lbound leadz len len_trim lenlge lge lgt lle llt log log10 logical lshift malloc matmul max max0 max1 maxexponent maxloc maxval merge min min0 min1 minexponent minloc minval mod modulo mvbits nearest nint not nworkers number_of_processors pack popcnt poppar precision present product radix random random_number random_seed range real repeat reshape rrspacing rshift scale scan secnds selected_int_kind selected_real_kind set_exponent shape sign sin sind sinh size sizeof sngl snglq spacing spread sqrt sum system_clock tan tand tanh tiny transfer transpose trim ubound unpack verify';
        var keywords    = 'access action advance allocatable allocate apostrophe assign assignment associate asynchronous backspace bind blank blockdata call case character class close common complex contains continue cycle data deallocate decimal delim default dimension direct do dowhile double doubleprecision else elseif elsewhere encoding end endassociate endblockdata enddo endfile endforall endfunction endif endinterface endmodule endprogram endselect endsubroutine endtype endwhere entry eor equivalence err errmsg exist exit external file flush fmt forall form format formatted function go goto id if implicit in include inout integer inquire intent interface intrinsic iomsg iolength iostat kind len logical module name named namelist nextrec nml none nullify number only open opened operator optional out pad parameter pass pause pending pointer pos position precision print private program protected public quote read readwrite real rec recl recursive result return rewind save select selectcase selecttype sequential sign size stat status stop stream subroutine target then to type unformatted unit use value volatile wait where while write';
    
        this.regexList = [
            { regex: new RegExp("C (.*)$","gmi"),                            css: 'comment' },
            { regex: dp.sh.RegexLib.SingleQuotedString,                        css: 'string' },
            { regex: new RegExp(this.GetKeywords(funcs), 'gmi'),                css: 'color2' },
            { regex: new RegExp(this.GetKeywords(keywords), 'gmi'),                css: 'keyword' }
            ];    
            
            this.CssClass='dp-fortran';
    };
    dp.sh.Brushes.Fortran.prototype = new dp.sh.Highlighter();
    dp.sh.Brushes.Fortran.Aliases=['fortran'];
    FORTRAN

    Just fyi - this uses the standard FORTRAN comments. I.E. -
    Code:
     C THIS IS A COMMENT
    As opposed to
    Code:
     // This is a comment or
    /* this is a comment or */
    # this is a comment or
    ! this is a comment...
    Here's a simple question: in order to add my own custom brush similar to what you have done here, do I have to be compiling from source, or can I somehow add this to my existing installation? Thank you.

  9. #9
    Join Date
    May 2009
    Location
    Houston, Tx
    Posts
    237

    Default

    Quote Originally Posted by jgriff4 View Post
    Here's a simple question: in order to add my own custom brush similar to what you have done here, do I have to be compiling from source, or can I somehow add this to my existing installation? Thank you.
    You can easily add this to an existing installation of Mindtouch. Simply write an extension (similar to my own) that adds a function to the syntax namespace.
    To err is human; to make real mess, you need a computer.
    Follow me on Twitter

  10. #10

    Default

    This is exactly what i was looking for. thank you for the informative post and keep up the good work!

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts