Typesetting:_Codestjohn_piano
The basic way to display code when writing an article is to enclose it within a code element. This is for code / data snippets and for the names of variables or tools. The code element will distinguish these visually from the surrounding text.
Example:
The \curl\ tool can be used to download a webpage, like so:
\curl http://edgecase.net/articles/0\
This will be rendered as:
The curl tool can be used to download a webpage, like so:
curl http://edgecase.net/articles/0
When writing, it is often easier to use code_lines instead, which can have a newline before and after the enclosed item, making the result easier to read and check.
Example:
The \curl\ tool can be used to download a webpage, like so:
\
curl http://edgecase.net/articles/0
\
This will be rendered as:
The curl tool can be used to download a webpage, like so:
curl http://edgecase.net/articles/0
The more complex way to display code is to use a lined_code element. This is designed to format a block of code (or an entire code file) for a web page in a readable way that can be manually copied / downloaded by the reader. The code itself should not be altered in any way, but simply copied into a new lined_code element, into the line just after the data_lines element.
Example:
\
\Hello World in Python\
\python_2.7.13\
\yes\
\
\asset\
\hello_world.py\
\[download this code]\
\a73a174cc9d68c2351153c7f2324690b80c9b58b46a7fef605ee9b5f3be12ed6\
\
\
#!/opt/local/bin/python
print "hello world"
\
\
Note: The asset link used here does not actually work. It is just an example.
This will be rendered as:
Hello World in Pythonpython_2.7.13yesassethello_world.py[download this code]a73a174cc9d68c2351153c7f2324690b80c9b58b46a7fef605ee9b5f3be12ed6
#!/opt/local/bin/python
print "hello world"
To avoid displaying line numbers, don't include the line_numbers element.
The link element is optional. If it is used, the code must be saved in a file and included as an asset of the article. This is for when the code is designed as a building block that will be used in the future. If it is just an example, made for illustration, there is no need to make it into an asset.
The title and language elements are also both optional.
If the line_numbers element exists and contains "yes", there is another optional element that can be included:
\21\
This is for ensuring that the correct line numbers are used when displaying an excerpt of a code file.
Example:
\
\yes\
\21\
\
n = g.order()
secret = randrange( 1, n )
pubkey = Public_key( g, g * secret )
privkey = Private_key( pubkey, secret )
\
\
This will be rendered as:
yes21
n = g.order()
secret = randrange( 1, n )
pubkey = Public_key( g, g * secret )
privkey = Private_key( pubkey, secret )
Here are several longer examples:
parse.asd
\
\parse.asd\
\Common_Lisp\
\
(asdf:defsystem :parse
:description ""
:author ""
:license ""
:version ""
:serial t
:components
((:file "packages")
(:file "error")
(:file "scope")
(:file "parse")))
\
\
This will be rendered as:
parse.asdCommon_Lisp
(asdf:defsystem :parse
:description ""
:author ""
:license ""
:version ""
:serial t
:components
((:file "packages")
(:file "error")
(:file "scope")
(:file "parse")))
packages.lisp
\
\packages.lisp\
\Common_Lisp\
\yes\
\
(defpackage :parse
(:use :cl)
(:export
*root-scope*
*active-scope*
scope-error
scope
scope-name
parent
grandparent
great-grandparent
errors
empty-p
has-errors-p
add-error
close-scope
execute-command
print-scope
content-scope
content
append-content
container-scope
children
open-child
keyword-scope
parse))
\
\
This will be rendered as:
packages.lispCommon_Lispyes
(defpackage :parse
(:use :cl)
(:export
*root-scope*
*active-scope*
scope-error
scope
scope-name
parent
grandparent
great-grandparent
errors
empty-p
has-errors-p
add-error
close-scope
execute-command
print-scope
content-scope
content
append-content
container-scope
children
open-child
keyword-scope
parse))
error.lisp
Note: The asset link used here does not actually work. It is just an example.
\
\error.lisp\
\Common_Lisp\
\yes\
\
\asset\
\error.lisp\
\[download this code]\
\a73a174cc9d68c2351153c7f2324690b80c9b58b46a7fef605ee9b5f3be12ed6\
\
\
(in-package :parse)
(defclass scope-error ()
((value
:initarg :value
:initform nil
:accessor value)))
(defclass no-function-error (scope-error)
((name
:initarg :name
:reader name)
(command
:initarg :command
:reader command)))
(defmethod initialize-instance
:after ((no-function-error no-function-error) &key)
(setf
(value no-function-error)
(format nil "No function for command \\"~A\\" in scope ~A"
(command no-function-error)
(name no-function-error))))
(defun make-no-function-error (name command)
(make-instance (quote no-function-error)
:name name
:command command))
\
\
This will be rendered as:
error.lispCommon_Lispyesasseterror.lisp[download this code]a73a174cc9d68c2351153c7f2324690b80c9b58b46a7fef605ee9b5f3be12ed6
(in-package :parse)
(defclass scope-error ()
((value
:initarg :value
:initform nil
:accessor value)))
(defclass no-function-error (scope-error)
((name
:initarg :name
:reader name)
(command
:initarg :command
:reader command)))
(defmethod initialize-instance
:after ((no-function-error no-function-error) &key)
(setf
(value no-function-error)
(format nil "No function for command \\"~A\\" in scope ~A"
(command no-function-error)
(name no-function-error))))
(defun make-no-function-error (name command)
(make-instance (quote no-function-error)
:name name
:command command))