【问题标题】:Using wikipedia lua modules in a script在脚本中使用 wikipedia lua 模块
【发布时间】:2021-03-25 19:14:26
【问题描述】:

好的,所以,让我首先明确一点,我不知道 Lua 的真正工作原理,从未在其中编写超过 10 行代码,而且我通常对 MediaWiki + Scribunto + Lua 的工作原理感到困惑.. .

所以...

我有这个维基词典模块/模板:

https://en.wiktionary.org/w/index.php?title=Module:pl-IPA&action=edit

在我看来,它就像是完全有效的 lua 代码。

当我尝试运行一些简单的示例(例如调用函数 export.convert_to_IPA("something"))时,Lua 似乎抱怨它不知道 mw 是什么。

据我所知,mw 代表 MediaWiki,是对 Scribunto 模块的引用。

问题是...如何在 Mac 上使用 Lua 解释器从终端运行的简单 Lua 脚本中找到并导入此 mw 模块?

【问题讨论】:

    标签: macos lua mediawiki scribunto


    【解决方案1】:

    一般来说,你不能在没有 MediaWiki 的独立 Lua 中使用 mw。它提供的几乎所有函数都是对 MediaWiki 的 PHP 的回调。但是,看起来您只想使用convert_to_IPA 而不是template_IPA,而前者唯一使用的mw 是这一行:for ch in mw.ustring.gmatch(mw.ustring.lower(word), ".") do。你很幸运:mw.ustring 是规则的例外,它确实有一个纯 Lua 实现:https://github.com/wikimedia/mediawiki-extensions-Scribunto/tree/master/includes/engines/LuaCommon/lualib/ustring

    这是一个如何使用它的示例(假设您的当前目录包含pl-IPA.lua 和我链接的ustring 目录):

    $ lua5.1
    Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
    > mw = {ustring = require 'ustring/ustring'}
    > export = require 'pl-IPA'
    > =export.convert_to_IPA("something")
    sɔˈmɛt.xʲink
    >
    

    【讨论】:

    • 非常感谢。很棒的答案。与此同时,我也发现了这个:(基本上,它是 Python 中维基词典解析器的一部分——几乎是我正在研究的,但在 Ruby 中)github.com/tatuylonen/wikitextprocessor
    猜你喜欢
    • 2017-07-22
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 2015-02-19
    • 2018-05-08
    • 2018-11-20
    • 2020-08-24
    • 2014-09-27
    相关资源
    最近更新 更多