bungle/lua-resty-hoedown

语言: Lua

git: https://github.com/bungle/lua-resty-hoedown

LuaJIT FFI绑定到Hoedown,这是一个符合标准,快速,安全的降价处理库。
LuaJIT FFI bindings to Hoedown, a standards compliant, fast, secure markdown processing library in C.
README.md (中文)

卢阿 - resty,农村舞会

lua-resty-hoedown是一个Markdown,SmartyPants,缓冲区,以及实现LuaJIT绑定的html和href / url转义库 农村舞会。

你好世界与lua-resty-hoedown

local hoedown = require "resty.hoedown"
hoedown[[
# Are you ready for the truth?

Now that there is the Tec-9, a crappy spray gun from South Miami.
This gun is advertised as the most popular gun in American crime.
Do you believe that shit? It actually says that in the little book
that comes with it: the most popular gun in American crime. Like
they're actually proud of that shit.

## I'm serious as a heart attack

The path of the righteous man is beset on all sides by the iniquities
of the selfish and the tyranny of evil men. Blessed is he who, in the
name of charity and good will, shepherds the weak through the valley
of darkness, for he is truly his brother's keeper and the finder of
lost children. And I will strike down upon thee with great vengeance
and furious anger those who would attempt to poison and destroy My
brothers.
]]

这将返回包含以下字符串:

<h1>Are you ready for the truth?</h1>

<p>Now that there is the Tec-9, a crappy spray gun from South Miami.
This gun is advertised as the most popular gun in American crime.
Do you believe that shit? It actually says that in the little book
that comes with it: the most popular gun in American crime. Like
they&#39;re actually proud of that shit.</p>

<h2>I&#39;m serious as a heart attack</h2>

<p>The path of the righteous man is beset on all sides by the iniquities
of the selfish and the tyranny of evil men. Blessed is he who, in the
name of charity and good will, shepherds the weak through the valley
of darkness, for he is truly his brother&#39;s keeper and the finder of
lost children. And I will strike down upon thee with great vengeance
and furious anger those who would attempt to poison and destroy My
brothers.</p>

安装

只需将resty目录放在package.path中的某个位置即可。如果您使用的是OpenResty,则默认位置为/ usr / local / openresty / lualib。 请检查您是否已经覆盖现有的resty目录。

编译和安装Hoedown C库

这些只是基本的注释。更好的安装说明如下:

  1. 首先从这里下载Hoedown:https://github.com/hoedown/hoedown
  2. 运行make
  3. 将libhoedown.so置于操作系统动态库的默认搜索路径中的某个位置(或修改resty / hoedown / library.lua和指向ffi_load(“libhoedown”)并使用libhoedown.so的完整路径。

使用LuaRocks或MoonRocks

如果您使用的是LuaRocks> = 2.2:

$ luarocks install lua-resty-hoedown

如果您使用的是LuaRocks <2.2:

$ luarocks install --server=http://rocks.moonscript.org moonrocks
$ moonrocks install lua-resty-hoedown

Lua-resty-hoedown的MoonRocks存储库位于:https://rocks.moonscript.org/modules/bungle/lua-resty-hoedown。

Lua API

API上有一些称为free的函数(完全实现Hoedown)。你真的不需要 call:free(),因为库使用普通的LuaJIT垃圾收集器自动处理释放外部资源 (库使用ffi.gc注册垃圾收集器处理程序)。

文档处理扩展

通过扩展,您可以扩展文档处理的工作方式。 以下是可用的扩展程序:

tables                = HOEDOWN_EXT_TABLES,
fenced_code           = HOEDOWN_EXT_FENCED_CODE,
footnotes             = HOEDOWN_EXT_FOOTNOTES,
autolink              = HOEDOWN_EXT_AUTOLINK,
strikethrough         = HOEDOWN_EXT_STRIKETHROUGH,
underline             = HOEDOWN_EXT_UNDERLINE,
highlight             = HOEDOWN_EXT_HIGHLIGHT,
quote                 = HOEDOWN_EXT_QUOTE,
superscript           = HOEDOWN_EXT_SUPERSCRIPT,
math                  = HOEDOWN_EXT_MATH,
no_intra_emphasis     = HOEDOWN_EXT_NO_INTRA_EMPHASIS,
space_headers         = HOEDOWN_EXT_SPACE_HEADERS,
math_explicit         = HOEDOWN_EXT_MATH_EXPLICIT,
disable_indented_code = HOEDOWN_EXT_DISABLE_INDENTED_CODE
local hoedown = require "resty.hoedown"
print(hoedown.document.extensions.tables)
local doc = require "resty.hoedown.document"
local extensions = doc.extensions
print(extensions.tables)

HTML呈现标志

使用HTML呈现标志,您可以控制HTML呈现过程。 野兔是可用的标志:

skip_html = HOEDOWN_HTML_SKIP_HTML,
escape    = HOEDOWN_HTML_ESCAPE,
hard_wrap = HOEDOWN_HTML_HARD_WRAP,
use_xhtml = HOEDOWN_HTML_USE_XHTML
local hoedown = require "resty.hoedown"
print(hoedown.html.flags.skip_html)
local html  = require "resty.hoedown.html"
local flags = html.flags
print(flags.use_xhtml)

HTML标记状态

这些当前值从resty.hoedown.html.is_tag函数返回。 可能的值是:

none  = HOEDOWN_HTML_TAG_NONE,
open  = HOEDOWN_HTML_TAG_OPEN,
close = HOEDOWN_HTML_TAG_CLOSE
local hoedown = require "resty.hoedown"
print(hoedown.html.tag.open)
local html = require "resty.hoedown.html"
local tag  = html.tag
print(tag.open)

resty.hoedown

使用单个语句可能需要的帮助程序库。

string hoedown(source,opts)

辅助函数来渲染。 source是包含源文档的字符串。 您还可以使用opts参数传递选项。以下是不同的选项 您可以使用(您也可以跳过传递选项):

opts = {
    renderer    = ("html" or "html.toc" or function or nil),
    extensions  = (table or number or nil),
    max_nesting = (number or nil),
    flags       = (table or number or nil),
    nesting     = (number or nil),
    smartypants = (true or false or nil)
}
local hoedown = require "resty.hoedown"
print(hoedown[[
# Hello World

Hi this is Markdown.
]])

local flags = hoedown.html.flags

print(hoedown([[
# Hello World

Hi this is Markdown.
]], {
    rendered    = "html.toc",
    nesting     = 1,
    flags       = { flags.use_xhtml, "escape", 1 },
    extensions  = { "underline", "quote" },
    smartypants = true
}))

字段

table resty.hoedown.buffer

这将返回一个缓冲区模块。

table resty.hoedown.document

这将返回一个文档模块。

table resty.hoedown.html

这将返回一个html模块。

table resty.hoedown.escape

这将返回一个转义模块。

table resty.hoedown.version

这将返回一个版本模块。

resty.hoedown.buffer

cdata buffer.context

string buffer.data

number buffer.size

number buffer.size

number buffer.unit

table buffer.new(size)

缓冲液:复位()

缓冲区:长(大小)

缓冲液:把(STR)

缓冲液:看跌(STR)

缓冲液:设置(STR)

缓冲液:套(STR)

布尔缓冲区:eq(str)

布尔缓冲区:eqs(str)

数字缓冲区:前缀(前缀)

缓冲液:啜食(大小)

cdata buffer:cstr()

buffer:printf(格式,...)

缓冲液:无()

数字缓冲区.__ len()

布尔缓冲区:__ eq()

字符串缓冲区:__ concat(x,y)

字符串缓冲区:__ tostring()

resty.hoedown.document

cdata document.context

table document.extensions

table document.new(渲染器,扩展,max_nesting)

字符串文档:渲染(数据)

字符串文档:render_inline(date)

文件:免费()

resty.hoedown.html

cdata html.context

表html.flags

table html.tag

string html.smartypants(data)

number html.is_tag(data,tag)

table html.new(flags,nesting)

HTML:免费()

表html.toc

表html.toc.new(嵌套)

html.toc:免费()

resty.hoedown.escape

字符串转义(来源,安全)

string escape.href(source)

string escape.html(来源,安全)

resty.hoedown.version

数字版本。主要

number version.minor

number version.revision

字符串版本:__ tostring()

执照

lua-resty-hoedown使用两个BSD许可证。

Copyright (c) 2014, Aapo Talvensaari
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this
  list of conditions and the following disclaimer in the documentation and/or
  other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

本文使用googletrans自动翻译,仅供参考, 原文来自github.com

en_README.md

lua-resty-hoedown

lua-resty-hoedown is a Markdown, SmartyPants, buffer, and html and href/url escaping library implementing LuaJIT bindings to
Hoedown.

Hello World with lua-resty-hoedown

local hoedown = require "resty.hoedown"
hoedown[[
# Are you ready for the truth?

Now that there is the Tec-9, a crappy spray gun from South Miami.
This gun is advertised as the most popular gun in American crime.
Do you believe that shit? It actually says that in the little book
that comes with it: the most popular gun in American crime. Like
they're actually proud of that shit.

## I'm serious as a heart attack

The path of the righteous man is beset on all sides by the iniquities
of the selfish and the tyranny of evil men. Blessed is he who, in the
name of charity and good will, shepherds the weak through the valley
of darkness, for he is truly his brother's keeper and the finder of
lost children. And I will strike down upon thee with great vengeance
and furious anger those who would attempt to poison and destroy My
brothers.
]]

This will return string containing:

<h1>Are you ready for the truth?</h1>

<p>Now that there is the Tec-9, a crappy spray gun from South Miami.
This gun is advertised as the most popular gun in American crime.
Do you believe that shit? It actually says that in the little book
that comes with it: the most popular gun in American crime. Like
they&#39;re actually proud of that shit.</p>

<h2>I&#39;m serious as a heart attack</h2>

<p>The path of the righteous man is beset on all sides by the iniquities
of the selfish and the tyranny of evil men. Blessed is he who, in the
name of charity and good will, shepherds the weak through the valley
of darkness, for he is truly his brother&#39;s keeper and the finder of
lost children. And I will strike down upon thee with great vengeance
and furious anger those who would attempt to poison and destroy My
brothers.</p>

Installation

Just place resty directory somewhere in your package.path. If you are using OpenResty, the default location would be /usr/local/openresty/lualib.
Please check though that you do not overwrite the existing resty-directory if one exists already.

Compiling and Installing Hoedown C-library

These are just rudimentary notes. Better installation instructions will follow:

  1. First download Hoedown from here: https://github.com/hoedown/hoedown
  2. Run make
  3. Place libhoedown.so somewhere in the default search path for dynamic libraries of your operating system (or modify resty/hoedown/library.lua and point ffi_load("libhoedown") with full path to libhoedown.so.

Using LuaRocks or MoonRocks

If you are using LuaRocks >= 2.2:

$ luarocks install lua-resty-hoedown

If you are using LuaRocks < 2.2:

$ luarocks install --server=http://rocks.moonscript.org moonrocks
$ moonrocks install lua-resty-hoedown

MoonRocks repository for lua-resty-hoedown is located here: https://rocks.moonscript.org/modules/bungle/lua-resty-hoedown.

Lua API

There are some functions called free on the API (to be complete implementation of Hoedown). You don't really need to
call :free() as the library automatically handles freeing the external resources using normal LuaJIT garbage collector
(the library registers the garbage collector handlers with ffi.gc).

Document Processing Extensions

With extensions, you may extend how to document processing works.
Here are the available extensions:

tables                = HOEDOWN_EXT_TABLES,
fenced_code           = HOEDOWN_EXT_FENCED_CODE,
footnotes             = HOEDOWN_EXT_FOOTNOTES,
autolink              = HOEDOWN_EXT_AUTOLINK,
strikethrough         = HOEDOWN_EXT_STRIKETHROUGH,
underline             = HOEDOWN_EXT_UNDERLINE,
highlight             = HOEDOWN_EXT_HIGHLIGHT,
quote                 = HOEDOWN_EXT_QUOTE,
superscript           = HOEDOWN_EXT_SUPERSCRIPT,
math                  = HOEDOWN_EXT_MATH,
no_intra_emphasis     = HOEDOWN_EXT_NO_INTRA_EMPHASIS,
space_headers         = HOEDOWN_EXT_SPACE_HEADERS,
math_explicit         = HOEDOWN_EXT_MATH_EXPLICIT,
disable_indented_code = HOEDOWN_EXT_DISABLE_INDENTED_CODE
Example
local hoedown = require "resty.hoedown"
print(hoedown.document.extensions.tables)
local doc = require "resty.hoedown.document"
local extensions = doc.extensions
print(extensions.tables)

HTML Rendering Flags

With HTML rendering flags you can control the HTML rendering process.
Hare are the available flags:

skip_html = HOEDOWN_HTML_SKIP_HTML,
escape    = HOEDOWN_HTML_ESCAPE,
hard_wrap = HOEDOWN_HTML_HARD_WRAP,
use_xhtml = HOEDOWN_HTML_USE_XHTML
Example
local hoedown = require "resty.hoedown"
print(hoedown.html.flags.skip_html)
local html  = require "resty.hoedown.html"
local flags = html.flags
print(flags.use_xhtml)

HTML Tag States

These present values returned from resty.hoedown.html.is_tag function.
The possible values are:

none  = HOEDOWN_HTML_TAG_NONE,
open  = HOEDOWN_HTML_TAG_OPEN,
close = HOEDOWN_HTML_TAG_CLOSE
Example
local hoedown = require "resty.hoedown"
print(hoedown.html.tag.open)
local html = require "resty.hoedown.html"
local tag  = html.tag
print(tag.open)

resty.hoedown

A helper library that you may require with a single statement.

string hoedown(source, opts)

Helper function to rendering. source is a string containing a source document.
You can also pass in options with opts argument. Here are the different options
that you may use (you can also skip passing options):

opts = {
    renderer    = ("html" or "html.toc" or function or nil),
    extensions  = (table or number or nil),
    max_nesting = (number or nil),
    flags       = (table or number or nil),
    nesting     = (number or nil),
    smartypants = (true or false or nil)
}
Example
local hoedown = require "resty.hoedown"
print(hoedown[[
# Hello World

Hi this is Markdown.
]])

local flags = hoedown.html.flags

print(hoedown([[
# Hello World

Hi this is Markdown.
]], {
    rendered    = "html.toc",
    nesting     = 1,
    flags       = { flags.use_xhtml, "escape", 1 },
    extensions  = { "underline", "quote" },
    smartypants = true
}))

Fields

table resty.hoedown.buffer

This returns a buffer module.

table resty.hoedown.document

This returns a document module.

table resty.hoedown.html

This returns an html module.

table resty.hoedown.escape

This returns an escape module.

table resty.hoedown.version

This returns a version module.

resty.hoedown.buffer

cdata buffer.context

string buffer.data

number buffer.size

number buffer.asize

number buffer.unit

table buffer.new(size)

buffer:reset()

buffer:grow(size)

buffer:put(str)

buffer:puts(str)

buffer:set(str)

buffer:sets(str)

boolean buffer:eq(str)

boolean buffer:eqs(str)

number buffer:prefix(prefix)

buffer:slurp(size)

cdata buffer:cstr()

buffer:printf(format, ...)

buffer:free()

number buffer.__len()

boolean buffer:__eq()

string buffer:__concat(x, y)

string buffer:__tostring()

resty.hoedown.document

cdata document.context

table document.extensions

table document.new(renderer, extensions, max_nesting)

string document:render(data)

string document:render_inline(date)

document:free()

resty.hoedown.html

cdata html.context

table html.flags

table html.tag

string html.smartypants(data)

number html.is_tag(data, tag)

table html.new(flags, nesting)

html:free()

table html.toc

table html.toc.new(nesting)

html.toc:free()

resty.hoedown.escape

string escape(source, secure)

string escape.href(source)

string escape.html(source, secure)

resty.hoedown.version

number version.major

number version.minor

number version.revision

string version:__tostring()

License

lua-resty-hoedown uses two clause BSD license.

Copyright (c) 2014, Aapo Talvensaari
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this
  list of conditions and the following disclaimer in the documentation and/or
  other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.