在脚本中使用维基百科lua模块

好的,让我们首先澄清一下,我不知道Lua真正的工作原理,从未在其中编写超过10行代码,并且对MediaWiki + Scribunto + Lua的工作方式感到困惑...

所以...

我有这个wiktionary模块/模板:

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

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

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

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

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

点赞
用户7509065
用户7509065

通常情况下,你无法在没有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  版权所有 (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
> 
2020-12-15 07:20:35