Plug-In Scripts

Since release 3.0 beta, highlight supports plug-in scripts which enhance existing language definitions and themes.

The following scripts serve as examples (you can find them in the plugins subdirectory of the highlight package):

plugin_cpp_syslog.lua

Adds additional keywords to C++ syntax description and corresponding formatting in colour theme

 1 --[[
 2 
 3 Sample plugin file for highlight 3.0
 4 
 5 Adds additional keywords to C++ syntax description and corresponding
 6 formatting in colour theme
 7 
 8 USAGE: highlight -I main.cpp --plug-in=plugin.lua
 9 ]]
10 
11 Description="Add syslog and kernel log levels to C and C++ keywords"
12 
13 -- function to update language definition with syslog levels
14 -- optional parameter: syntax description
15 function syntaxUpdate(desc)
16   if desc=="C and C++" then
17     -- insert syslog level for C and C++
18     table.insert( Keywords,
19                   { Id=5, List={"LOG_EMERG", "LOG_CRIT", "LOG_ALERT",
20                     "LOG_ERR", "LOG_WARNING","LOG_NOTICE","LOG_INFO",
21                     "LOG_DEBUG",
22                     "KERN_ERR", "KERN_INFO", "KERN_EMERG", "KERN_ALERT",
23                     "KERN_CRIT",  "KERN_WARNING",  "KERN_NOTICE",
24                     "KERN_DEBUG"  }
25                   } )
26   end
27 end
28 
29 -- function to update theme definition
30 -- optional parameter: theme description
31 function themeUpdate(desc)
32 
33   -- change background colour of kwrite theme
34   if desc=="Kwrite Editor" then
35     Canvas={ Colour="#E0EAEE" }
36   end
37 
38   --add 5th keyword style for syslog levels defined in  syntaxUpdate()
39   table.insert(Keywords, {Colour= "#ff0000", Bold=true})
40 end
41 
42 
43 --The Plugins array assigns code chunks to themes or language definitions.
44 --Chunks may be functions or strings with Lua code.
45 --The chunks are interpreted after the theme or lang file were parsed,
46 --so you can refer to elements of these files
47 
48 Plugins={
49 
50   --{ Type="theme", Chunk="LineComment={ Colour=\"#ababab\" }" }, -- string example
51   { Type="theme", Chunk=themeUpdate },
52   { Type="lang", Chunk=syntaxUpdate },
53 
54 }

plugin_cpp_qt.lua

Adds additional keywords to C++ syntax description and corresponding formatting in colour theme

 1 --[[
 2 
 3 Sample plugin file for highlight 3.0
 4 
 5 Adds additional keywords to C++ syntax description and corresponding
 6 formatting in colour theme
 7 
 8 USAGE: highlight -I main.cpp --plug-in=plugin.lua
 9 ]]
10 
11 Description="Add Qt keywords to C and C++ definition"
12 
13 -- function to update language definition with syslog levels
14 -- optional parameter: syntax description
15 function syntaxUpdate(desc)
16   if desc=="C and C++" then
17     -- insert Qt keywords
18 	table.insert( Keywords,
19                   { Id=1, List={"slots" }
20                   } )
21 	table.insert( Keywords,
22                   { Id=2, Regex=[[Q\p{Upper}\w+]]
23                   } )
24 	table.insert( Keywords,
25                   { Id=5, List={"SIGNAL", "SLOT"}
26                   } )
27 	table.insert( Keywords,
28                   { Id=5, Regex=[[Q_[\p{Upper}_]+]]
29                   } )
30   end
31 end
32 
33 -- function to update theme definition
34 -- optional parameter: theme description
35 function themeUpdate(desc)
36   table.insert(Keywords, {Colour= "#ff0000", Bold=true})
37 end
38 
39 
40 --The Plugins array assigns code chunks to themes or language definitions.
41 --Chunks may be functions or strings with Lua code.
42 --The chunks are interpreted after the theme or lang file were parsed,
43 --so you can refer to elements of these files
44 
45 Plugins={
46 
47   { Type="theme", Chunk=themeUpdate },
48   { Type="lang", Chunk=syntaxUpdate },
49 
50 }

outhtml_add_shadow.lua

01 
02 Description="Adds a shadow and margin to the code box (intended for bright themes)"
03 
04 -- function to update theme definition
05 -- optional parameter: theme description
06 function themeUpdate()
07 
08   function lighten(colour)
09     if string.match(colour, "#%x+")==nil then
10       return "#ffffff"
11     end
12     rr=50 + ("0x"..string.match(colour, "%x%x", 2))
13     gg=50 + ("0x"..string.match(colour, "%x%x", 4))
14     bb=50 + ("0x"..string.match(colour, "%x%x", 6))
15     if (rr>255) then rr = 255 end
16     if (gg>255) then gg = 255 end
17     if (bb>255) then bb = 255 end
18     return string.format("#%02x%02x%02x", rr, gg, bb)
19   end
20 
21   if (HL_OUTPUT == HL_FORMAT_HTML or HL_OUTPUT == HL_FORMAT_XHTML) then
22     Injections[#Injections+1]="pre.hl, ol.hl { margin:2em; box-shadow:10px 10px 0.5em "
23         ..lighten(Default.Colour).."; border: 1px solid "..lighten(Default.Colour)..";}"
24     
25     Injections[#Injections+1]="pre.hl {padding:1em;}"
26     Injections[#Injections+1]="ol.hl {padding:1em 1em 1em 4em;}"
27     Injections[#Injections+1]="body.hl { background-color:"..lighten(Canvas.Colour).."; }"   
28   end
29  
30 end
31 
32 Plugins={
33 
34   { Type="theme", Chunk=themeUpdate }
35 
36 }

outhtml_ref_qtproject.org.lua

01 --[[
02 Sample plugin file for highlight 3.9
03 ]]
04 
05 Description="Add qtproject.org reference links to HTML, LaTeX, RTF and ODT output of C++ code"
06 
07 -- optional parameter: syntax description
08 function syntaxUpdate(desc)
09 
10   if desc~="C and C++" then
11      return
12   end
13       
14   function getURL(token)
15      url='http://qt-project.org/doc/qt-5/'..string.lower(token).. '.html'
16      
17      if (HL_OUTPUT== HL_FORMAT_HTML or HL_OUTPUT == HL_FORMAT_XHTML) then
18         return '<a class="hl" target="new" href="' .. url .. '">'.. token .. '</a>'
19       elseif (HL_OUTPUT == HL_FORMAT_LATEX) then
20         return '\\href{'..url..'}{'..token..'}'
21       elseif (HL_OUTPUT == HL_FORMAT_RTF) then
22         return '{{\\field{\\*\\fldinst HYPERLINK "'..url..'" }{\\fldrslt\\ul\\ulc0 '..token..'}}}'
23       elseif (HL_OUTPUT == HL_FORMAT_ODT) then
24         return '<text:a xlink:type="simple" xlink:href="'..url..'">'..token..'</text:a>'
25      end
26    end
27 
28   function Decorate(token, state)
29     if (state ~= HL_STANDARD and state ~= HL_KEYWORD and state ~=HL_PREPROC) then
30       return
31     end
32     if string.find(token, "Q%u%l")==1 then
33       return getURL(token)
34     end
35   end
36   
37 end
38 
39 function themeUpdate(desc)
40   if (HL_OUTPUT == HL_FORMAT_HTML or HL_OUTPUT == HL_FORMAT_XHTML) then
41     Injections[#Injections+1]="a.hl, a.hl:visited {color:inherit;font-weight:inherit;}"
42   elseif (HL_OUTPUT==HL_FORMAT_LATEX) then
43     Injections[#Injections+1]="\\usepackage[colorlinks=false, pdfborderstyle={/S/U/W 1}]{hyperref}"
44   end
45 end
46 
47 Plugins={
48 
49   { Type="lang", Chunk=syntaxUpdate },
50   { Type="theme", Chunk=themeUpdate },
51 
52 }

plug-in_scripts.txt · Last modified: 2016/05/27 10:32 by saalen
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0