wiki:plugins-demo/CodeExampleMacro

CodeExampleMacro: 代码语法高亮显示

示例

简单的示例

基本的特征包括:

  1. 通过SELECT ALL选中全部代码;
  2. 代码显示区域可以折叠收起;
  3. 使用 #!<语言名称> 指定代码语言;
  4. 使用 ##title = ... 指定标题;
SELECT ALL 代码示例:
@staticmethod
def get_templates_dirs():
    """ Notify Trac about templates dir. """
    from pkg_resources import resource_filename
    return [resource_filename(__name__, 'templates')]

##type参数

支持 ##type 参数: simple, good, bad , 用于显示标识为正确或者错误的代码

SELECT ALL 错误代码:
fibs = 0 : 1 : [ a + b | a <- fibs | b <- tail fibs ]
SELECT ALL 良好的代码:
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

使用文档

[[CodeExample]]

Render a code example box that supports syntax highlighting. It support three types of examples: simple, correct, and incorrect. The SELECT ALL link highlights all of the code in the box to simplify the copy and paste action.

The simple example:

{{{
#!CodeExample
#!python
@staticmethod
def get_templates_dirs():
    """ Notify Trac about templates dir. """
    from pkg_resources import resource_filename
    return [resource_filename(__name__, 'templates')]
}}}

will be rendered as:

/chrome/ce/img/example1.png

The incorrect example:

{{{
#!CodeExample
## type = bad
#!haskell
fibs = 0 : 1 : [ a + b | a <- fibs | b <- tail fibs ]
}}}

will be rendered as:

/chrome/ce/img/example2.png

The correct example:

{{{
#!CodeExample
## type = good
#!haskell
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
}}}

will be rendered as:

/chrome/ce/img/example3.png

There is also support for getting sources from the repository:

{{{
#!CodeExample
## path=GPGMail/Source/GPGMailPreferences.m
## regex=".*updater\s*{"
## lines=3
#!objective-c
}}}

will be rendered as:

/chrome/ce/img/example4.png

Parameters:

  • type - (optional) a type of the example: simple (default), good, bad
  • title - (optional) the title of the example
  • path - (optional) a file in the repository (using TracLinks format for source code)
  • repo - (optional) repository to use (Trac 0.12 and upper only)
  • regex - (optional) a regular expression indicates where to start an example
  • lines - (optional) number of lines to show

参考

  • http://trac-hacks.org/wiki/CodeExampleMacro
  • 来自上述地址的语法摘要:
    Argument
    Version
    Required (R)
    Optional (O)
    Description
    Default
    ## type O A type of box example: simple, good, bad
    * Example: ## type = bad
    simple
    ## title O The title of the example.
    * Example: ## title = Source from repository A
    If ## type = simple then EXAMPLE:
    If ## type = good then CORRECT EXAMPLE:
    If ## type = bad then INCORRECT EXAMPLE:
    ## path O A file in the repository (using TracLinks format for source code)
    * Example: ## path = GPGMail/Source/GPGMailPreferences.m
    None
    ## repo O Repository to use (Trac 0.12 and upper only).
    * Example: ## repo = T.B.D.
    None
    ## regex O A regular expression indicates where to start an example.
    * Example: ## regex = ".*updater\s*{"
    None
    ## lines O Number of lines to show.
    * Example: ## lines = 3
    None
    #!language O Syntax highlighter. language is any supported by Trac. See TracSyntaxColoring.
    * Example: #!python
    trac
Last modified 5 years ago Last modified on 2013-01-05T00:06:51+08:00