扩展¶
标准扩展¶
CoffeeScript¶
Plim 使用 Python-CoffeeScript 包作为 JS 与 CoffeeScript 之间的桥梁。你可以使用 -coffee
作为 CoffeeScript 代码块标记。
- coffee
# Assignment:
number = 42
opposite = true
# Conditions:
number = -42 if opposite
# Functions:
square = (x) -> x * x
# Arrays:
list = [1, 2, 3, 4, 5]
# Objects:
math =
root: Math.sqrt
square: square
cube: (x) -> x * square x
# Splats:
race = (winner, runners...) ->
print winner, runners
# Existence:
alert "I knew it!" if elvis?
# Array comprehensions:
cubes = (math.cube num for num in list)
SCSS/SASS¶
Plim 使用 pyScss 包将 SCSS/SASS 翻译为普通的 CSS。你可以使用 -scss
或 -sass
作为 SCSS/SASS 代码块标记。输出结果中将会自动包裹在 <style></style>
标签中。
例如:
- scss
@option compress: no;
.selector {
a {
display: block;
}
strong {
color: blue;
}
}
输出:
<style>.selector a {
display: block;
}
.selector strong {
color: #00f;
}</style>
Stylus¶
Plim uses stylus package to translate
stylus markup to plain CSS.
You can start Stylus block with the -stylus
construct. The output will be
wrapped with <style></style>
tags.
例如:
- stylus
@import 'nib'
body
background: linear-gradient(top, white, black)
border-radius()
-webkit-border-radius arguments
-moz-border-radius arguments
border-radius arguments
a.button
border-radius 5px
输出:
<style>body {
background: -webkit-gradient(linear, left top, left bottom, color-stop(0, #fff), color-stop(1, #000));
background: -webkit-linear-gradient(top, #fff 0%, #000 100%);
background: -moz-linear-gradient(top, #fff 0%, #000 100%);
background: -o-linear-gradient(top, #fff 0%, #000 100%);
background: -ms-linear-gradient(top, #fff 0%, #000 100%);
background: linear-gradient(top, #fff 0%, #000 100%);
}
a.button {
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}</style>
Markdown¶
Plim 使用 python-markdown2 包处理 -markdown
(或者 -md
)扩展
例如:
- markdown
A First Level Header
====================
A Second Level Header
---------------------
Now is the time for all good men to come to
the aid of their country. This is just a
regular paragraph.
The quick brown fox jumped over the lazy
dog's back.
### Header 3
> This is a blockquote.
>
> This is the second paragraph in the blockquote.
>
> ## This is an H2 in a blockquote
输出:
<h1>A First Level Header</h1>
<h2>A Second Level Header</h2>
<p>Now is the time for all good men to come to
the aid of their country. This is just a
regular paragraph.</p>
<p>The quick brown fox jumped over the lazy
dog's back.</p>
<h3>Header 3</h3>
<blockquote>
<p>This is a blockquote.</p>
<p>This is the second paragraph in the blockquote.</p>
<h2>This is an H2 in a blockquote</h2>
</blockquote>
reStructuredText¶
Plim 使用 Docutils 包 来处理 -rest
(或者 -rst
)扩展以及项目文档。
例如:
- rest
Grid table:
+------------+------------+-----------+
| Header 1 | Header 2 | Header 3 |
+============+============+===========+
| body row 1 | column 2 | column 3 |
+------------+------------+-----------+
| body row 2 | Cells may span columns.|
+------------+------------+-----------+
| body row 3 | Cells may | - Cells |
+------------+ span rows. | - contain |
| body row 4 | | - blocks. |
+------------+------------+-----------+
输出:
<p>Grid table:</p>
<table border="1">
<thead valign="bottom">
<tr>
<th>Header 1
</th><th>Header 2
</th><th>Header 3
</th></tr>
</thead>
<tbody valign="top">
<tr>
<td>body row 1
</td><td>column 2
</td><td>column 3
</td></tr>
<tr>
<td>body row 2
</td><td colspan="2">Cells may span columns.
</td></tr>
<tr>
<td>body row 3
</td><td rowspan="2">Cells may<br>span rows.
</td><td rowspan="2">
<ul>
<li>Cells
</li><li>contain
</li><li>blocks.
</li></ul>
</td></tr>
<tr>
<td>body row 4
</td></tr>
</tbody></table>
Handlebar¶
handlebars
是 Plim 支持的一个特殊标签,将会翻译为 handlebars
:
<script type="text/x-handlebars"></script>
这对 Ember.js 开发者来说非常便利。
例如,下面的 Plim 文档
html
body
handlebars#testapp
.container {{outlet}}
handlebars#about: .container {{outlet}}
将会成为
<html>
<body>
<script type="text/x-handlebars" id="testapp">
<div class="container">{{outlet}}</div>
</script>
<script type="text/x-handlebars" id="about">
<div class="container">{{outlet}}</div>
</script>
</body>
</html>
使用定制的分析器扩展 Plim¶
0.9.2 新版功能.
你也可以自己扩展 Plim 标签,该功能允许你在 Plim 之上定义自己的 DSL。例如,下面的代码将定义一个新的 HTML 链接解释器,将会处理 http_url > title
形式的链接。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # my_module.py
import re
from plim import preprocessor_factory
PARSE_HTTP_LINKS_RE = re.compile('(?P<url>https?://[^>]+)+\s+>\s+(?P<title>.*)')
def parse_http_link(indent_level, current_line, matched, source, syntax):
url = matched.group('url')
url_title = matched.group('title')
rt = '<a href="{}">{}</a>'.format(url, url_title)
return rt, indent_level, '', source
CUSTOM_PARSERS = [
(PARSE_HTTP_LINKS_RE, parse_http_link)
]
custom_preprocessor = preprocessor_factory(custom_parsers=CUSTOM_PARSERS, syntax='mako')
|
parse_http_link()
函数的定义严格地遵循了 Plim API,
所有扩展解释器都应该接受 5 个参数:
indent_level
- 当前行的缩进层次。分析器发现某一行的缩进比上一行多indent_level
层或更多时,将把它的控制权交给上一层。current_line
- 已被解析的行列。说明这一行在上一步的解析中已经被matched
解析器匹配。matched
- an instance ofre.MatchObject
of the regex associated with the current parser.source
-plim.lexer.enumerate_source()
所返回的枚举对象实例。syntax
-plim.syntax.BaseSyntax
子类的实例。
并返回包含以下元素的元组:
parsed_data - 解释器处理后的字符串。
tail_indent -
tail line
的缩进等级。tail_line - 缩进等级(
tail_indent
)小于或等于indent_level
的行。source
-plim.lexer.enumerate_source()
返回的枚举对象。包含尚未处理的 plim 标记。
现在我们已经可以像使用标准 Plim 预处理器 plim.preprocessor
一样使用 custom_preprocessor
了。
让我们使用新定义的语法来创建一个 plim 文档:
1 2 3 4 5 6 7 8 9 | / hamilton.plim
---------------
html
head:title Alexander Hamilton
body
h1 Alexander Hamilton
ul
li: http://en.wikipedia.org/wiki/Alexander_Hamilton > Wikipedia Article
li: http://www.amazon.com/Alexander-Hamilton-Ron-Chernow/dp/0143034758 > Full-length Biography
|
将会输出这样的有效 HTML (注意 -p
参数):
$ plimc -H -p my_module:custom_preprocessor hamilton.plim
输出结果:
1 2 3 4 5 6 7 8 9 10 11 12 | <html>
<head>
<title>Alexander Hamilton</title>
</head>
<body>
<h1>Alexander Hamilton</h1>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Alexander_Hamilton">Wikipedia Article</a></li>
<li><a href="http://www.amazon.com/Alexander-Hamilton-Ron-Chernow/dp/0143034758">Full-length Biography</a></li>
</ul>
</body>
</html>
|