• 3033 views
  • 0 comments
  • loading...

Performs syntax highlighting for Python, C, C++, HTML and web2py code,

This helper performs syntax highlighting for Python, C, C++, HTML and web2py code, and is preferable to PRE for code listings. CODE also has the ability to create links to the web2py API documentation.

These are the default arguments for the CODE helper:

CODE("print 'hello world'", language='python', link=None, counter=1, styles={})
Supported values for the language argument are "python", "html_plain", "c", "cpp", "web2py", and "html". The "html" language interprets {{ and }} tags as "web2py" code, while "html_plain" doesn't.

























Here is an example of highlighting sections of Python code.


>>>
print CODE('print "hello"', language='python').xml()
<
table><tr valign="top"><td style="width:40px; text-align: right;"><pre style="
font-size: 11px;
font-family: Bitstream Vera Sans Mono,monospace;
background-color: transparent;
margin: 0;
padding: 5px;
border: none;
background-color: #E0E0E0;
color: #A0A0A0;
">1.</pre></td><td><pre style="
font-size: 11px;
font-family: Bitstream Vera Sans Mono,monospace;
background-color: transparent;
margin: 0;
padding: 5px;
border: none;
overflow: auto;
"><span style="color:#185369; font-weight: bold">print </span>
<span style="color: #FF9966">"hello"</span></pre></td></tr>
</
table>

Attributes / Arguments

Supported values for the language argument are:
 "python", "html_plain", "c", "cpp", "web2py", and "html". 

The "html" language interprets {{ and }} tags as "web2py" code, while "html_plain" doesn't.

CODE("print 'hello world'", language='python')

The counter argument is used for line numbering. It can be set to any of three different values. It can be None for no line numbers, a numerical value specifying the start number, or a string. If the counter is set to a string, it is interpreted as a prompt, and there are no line numbers.

CODE("print 'hello world'", language='python', link=None, counter=1)

The styles argument is a bit tricky. If you look at the generated HTML above, it contains a table with two columns, and each column has its own style declared inline using CSS. The styles attributes allows you to override those two CSS styles. For example:
{{=CODE(...,styles={'CODE':'margin: 0;padding: 5px;border: none;'})}}
The styles attribute must be a dictionary, and it allows two possible keys: CODE for the style of the actual code, and LINENUMBERS for the style of the left column, which contains the line numbers. Mind that these styles completely replace the default styles and are not simply added to them.

{{=CODE(context_line=False,
    styles={
    'LINENUMBERS':'visibility: hidden;'
   ,'CODE':'font-size: 14px; background-color: transparent;margin: 0 0 0    -40px;        padding: 5px 10px 10px 10px;border: none;verflow: auto;white-space: pre !important;'
})}}

This argument highlights the line.

 {{=CODE(reference.code, language='web2py', link=None, counter=None,context_line=False


loading...

Sources / Reference

" Web2py Book"


Comments

loading...

Powered by
Web2py

Hosted on
www.pythonanywhere.com
(affiliated link)