liquid-lua

Liquid 的 Lua 实现

$ opm get chenxianyu2015/liquid-lua

liquid-lua

Liquid 的 Lua 实现,用于 OpenResty 平台。 [!构建状态](https://travis-ci.org/chenxianyu2015/liquid-lua)

介绍

由于 Liquid 标记语言没有官方语言规范文档,liquid-lua 采用经典的词法分析器/解析器/解释器实现方法,并采用严格错误模式,这与 liquid-ruby 方法(正则表达式词法分析器/标签解析器/标签解释器)的三种错误模式(宽松/警告/严格模式)不同。 由于实现方法不同,liquid-lua 在某些情况下会与 liquid-ruby 的行为不同。也就是说,liquid-lua 不完全兼容 liquid-ruby。 关于 liquid-lua 和 liquid-ruby 之间不兼容的更多详细信息将在 DIFFERENCE.md 中收集和列出。 liquid-lua 的以下组件已实现

  • 词法分析器

  • 解析器

  • 解释器

  • 解析器上下文 用于空格控制功能的组件。

  • 解释器上下文 向 Liquid 引擎公开 Lua 变量的组件。

  • 过滤器集 向 Liquid 引擎公开 Lua 函数的组件。 在 liquid-ruby 中,它被称为 Strainer

  • 资源限制 用于 Liquid 引擎的简单系统资源限制组件。

  • 文件系统 模板源后端(Redis/Mongo/本地文件系统)的简单包装器。

  • 延迟 使普通 Lua 表格像延迟哈希一样工作的包装器。 在 liquid-ruby 中,它被称为 Drop

  • 节点表 运行时错误可以从节点表中获取相应的源文本位置信息。(Linux ELF 有一个 Systab,liquid-lua 有一个 Nodetab。)

安装

使用 OpenResty 包管理器(推荐)

  1. 如果您还没有安装 OpenResty 包管理器,请先安装;

  2. 运行以下 Openesty 包管理器命令以搜索和安装 liquid-lua 包。

    opm search liquid-lua
    opm get chenxianyu2015/liquid-lua

手动安装

  1. 下载 zip 包。

  2. 从 zip 包中解压缩 liquid.lua 目录。

  3. 将其复制到您的 OpenResty Lua 库路径。

用法

目前 liquid-lua 只有很多基本和底层的组件,它们不友好或不容易使用。 但是,使用这些组件很容易构建友好的接口。 以下是一些示例,展示如何使用这些组件。

  1. 一个“hi tobi”示例

        -- require these components
        local Liquid = require 'liquid'
        local Lexer = Liquid.Lexer
        local Parser = Liquid.Parser
        local Interpreter = Liquid.Interpreter
        local InterpreterContext = Liquid.InterpreterContext
        
        -- template to render
        local document = 'hi {{name}}'
        -- variable to render
        local var = {["name"] = "tobi" }
        
        -- dataflow: lexer -> parser -> interpreter
        local lexer = Lexer:new(document)
        local parser = Parser:new(lexer)
        local interpreter = Interpreter:new(parser)
        
        -- the content of result is  'hi tobi'
        local result = interpreter:interpret( InterpreterContext:new(var) )
  1. 一个 for 循环示例

        -- require these components
        local Liquid = require 'liquid'
        local Lexer = Liquid.Lexer
        local Parser = Liquid.Parser
        local Interpreter = Liquid.Interpreter
        local InterpreterContext = Liquid.InterpreterContext
        
        -- template to render
        local document = '{%for k in num%} {{k}} {% endfor%}'
        -- variable to render
        local var = {["num"] = {5,4,3,2,1} }
        
        -- dataflow: lexer -> parser -> interpreter
        local lexer = Lexer:new(document)
        local parser = Parser:new(lexer)
        local interpreter = Interpreter:new(parser)
        
        -- the content of result is:  5  4  3  2  1
        local result = interpreter:interpret( InterpreterContext:new(var) )

待办事项

  • 更多测试用例

  • 重复和脏代码清理和重构

  • 模板缓存和 AST 缓存组件

  • 更多过滤器

参与

  • 报告错误GitHub 问题 中发布描述、完整堆栈跟踪和所有相关代码。

许可证

此模块在 BSD 许可下获得许可。

作者

chenxianyu2015

许可证

2bsd

版本