Syntax highlighting

From VLECK
Jump to: navigation, search

Syntax highlighting makes the source code better readable. On Wiki pages, it is achieved by surrounding code snippets with

{{sourcecode|caption=...|language=...|code=....}}


For the language-parameter, for example "python", "java", "delphi", "xml", etc may be subsituted. "NSM" has now been enabled too.

For the most frequent languages even more direct templates can be used, e.g.

{{xml|caption=...|code=....}}
{{NSM|caption=...|code=....}}
{{python|caption=...|code=....}}


examples

For example, three times this same python function:

 
@final
def integration_beforeIntegration(self):
  self.beforeIntegration()
  for m in self._getMembers():
    m.integration_beforeIntegration()


without formatting (a mess):
@final def integration_beforeIntegration(self):

 self.beforeIntegration()
 for m in self._getMembers():
   m.integration_beforeIntegration()

within <pre> ... </pre>

  @final
  def integration_beforeIntegration(self):
    self.beforeIntegration()
    for m in self._getMembers():
      m.integration_beforeIntegration()

(the same formatting you'll get when each line starts with one or more blanks).


with syntax highlighting (using the python-template):

 
@final
  def integration_beforeIntegration(self):
    self.beforeIntegration()
    for m in self._getMembers():
      m.integration_beforeIntegration()



The wiki-source of the example above is:

{{python|code=
  @final
  def integration_beforeIntegration(self):
    self.beforeIntegration()
    for m in self._getMembers():
      m.integration_beforeIntegration()
}}

language examples

xml
<type name="PlantOrgan" implementation="PlantComponent">
         <parameter name="cAllocNPP">0.5</parameter>
		 <state name="carbon"   units="mol">1.0</state>
		 <state name="ammonium" units="mol">1.0</state>
		 <state name="nitrate"  units="mol">1.0</state>
	  </type>



python
def getAttributesOfObject(o, exclusion_prefix="_"):
  """
  return all "normal" attributes of an object
  @param o: object
  @param exclusion_prefix: leading characters of excluded attribute names (default underscore)
  @return: dictionary
  """
  result = {}
  for name in dir(o):
    value = getattr(o, name)    
    if not (inspect.isclass(value) 
         or inspect.ismethod(value) 
         or inspect.isfunction(value) 
         or inspect.isbuiltin(value)
         or name.startswith(exclusion_prefix)
         or str(type(value)) == "<type 'method-wrapper'>"):
      result[name] = value
  return result



NSM
Function OpenSharedDatabase(aFileName)
 
    if (FileExists(aFileName))
        DeleteFile(aFileName);
    else
        path = ExtractFilePath(aFileName);
        if (not(FileExists(path)))
            DOS("mkdir """  + path + """");
        endif
    endif
 
    handle              = CreateDatabase(aFileName);    // opened in unshared mode
    CloseDatabase(handle);
    OpenSharedDatabase  = OpenDatabase(aFileName);      // open in shared mode
 
End of OpenSharedDatabase
 
 
function WriteX(aText)
    #IFNDEF _SUPPRESS_SCREEN_OUTPUT
        Write( aText );
    #ENDIF
end of WriteX



more information

http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi