|
2 | 2 |
|
3 | 3 | import org.luaj.vm2.LuaTable; |
4 | 4 | import org.luaj.vm2.LuaValue; |
| 5 | +import org.luaj.vm2.LuaInteger; |
5 | 6 | import org.luaj.vm2.Varargs; |
6 | 7 | import org.luaj.vm2.lib.LibFunction; |
7 | 8 | import org.luaj.vm2.lib.OneArgFunction; |
8 | 9 | import org.luaj.vm2.lib.TwoArgFunction; |
9 | 10 |
|
10 | 11 | import wiki.tools.WikiPage; |
| 12 | +import static wiki.NameSpaces.getNameSpaceByNumber; |
| 13 | +import static wiki.NameSpaces.getNameSpaceNumber; |
11 | 14 |
|
12 | 15 | import static info.bliki.extensions.scribunto.template.Frame.toLuaString; |
13 | 16 |
|
@@ -45,7 +48,7 @@ public LuaTable getInterface() { |
45 | 48 | @Override |
46 | 49 | public LuaValue getSetupOptions() { |
47 | 50 | LuaTable table = new LuaTable(); |
48 | | - table.set("thisTitle", title("", wp.getPagename())); |
| 51 | + table.set("thisTitle", title(LuaValue.valueOf(0), toLuaString(wp.getPagename()), LuaValue.NIL, LuaValue.NIL)); |
49 | 52 | table.set("NS_MEDIA", -2); |
50 | 53 | return table; |
51 | 54 | } |
@@ -142,16 +145,18 @@ public LuaValue call(LuaValue text_or_id, LuaValue defaultNamespace) { |
142 | 145 | if (isValidTitle(text_or_id, defaultNamespace)) { |
143 | 146 | String text = text_or_id.checkjstring(); |
144 | 147 | String interwiki = null; |
| 148 | + Integer ns_id = null; |
145 | 149 | int idx = text.indexOf(":"); |
146 | 150 | if (idx != -1) { |
147 | 151 | String prefix = text.substring(0, idx); |
148 | 152 | if (prefix.equals("w") || prefix.equals("wikipedia") || prefix.equals("wikt") || prefix.equals("wiktionary"))//to be extended with more interwikis |
149 | 153 | interwiki = prefix; |
| 154 | + else ns_id = getNameSpaceNumber(prefix);//ns_id != null in case prefix is namespace |
150 | 155 | } |
151 | 156 | int fragment_idx = text.indexOf("#"); |
152 | 157 | return title( |
153 | | - defaultNamespace, |
154 | | - text_or_id, |
| 158 | + ns_id == null ? defaultNamespace : LuaInteger.valueOf(ns_id), |
| 159 | + ns_id == null ? text_or_id : toLuaString(text.substring(idx + 1)), |
155 | 160 | fragment_idx == -1 ? NIL : toLuaString(text.substring(fragment_idx + 1)), |
156 | 161 | interwiki == null ? NIL : toLuaString(interwiki)); |
157 | 162 | } else { |
@@ -202,32 +207,21 @@ public Varargs invoke(Varargs args) { |
202 | 207 | }; |
203 | 208 | } |
204 | 209 |
|
205 | | - |
206 | | - private LuaValue title(String namespace, String pageName) { |
207 | | - return title( |
208 | | - toLuaString(namespace != null ? namespace : ""), |
209 | | - toLuaString(pageName != null ? pageName : ""), |
210 | | - LuaValue.NIL, |
211 | | - LuaValue.NIL |
212 | | - ); |
213 | | - } |
214 | | - |
215 | 210 | private LuaValue title(LuaValue ns, LuaValue title, LuaValue fragment, LuaValue interwiki) { |
216 | 211 | LuaTable table = new LuaTable(); |
217 | 212 | table.set("isLocal", EMPTYSTRING); |
218 | 213 | table.set("isRedirect", EMPTYSTRING); |
219 | 214 | table.set("subjectNsText", EMPTYSTRING); |
220 | 215 | table.set("interwiki", interwiki.isnil() ? EMPTYSTRING : interwiki); |
221 | | - table.set("namespace", LuaValue.valueOf(0)); |
222 | | - table.set("nsText", ns.isnil() ? LuaValue.EMPTYSTRING : ns); |
| 216 | + table.set("namespace", ns.isnil() ? LuaValue.valueOf(0) : ns); |
| 217 | + table.set("nsText", ns.isnil() ? LuaValue.EMPTYSTRING : toLuaString(getNameSpaceByNumber(ns.checkint()))); |
223 | 218 | table.set("text", title); |
224 | 219 | table.set("id", title); |
225 | 220 | table.set("fragment", fragment.isnil() ? EMPTYSTRING : fragment); |
226 | 221 | table.set("contentModel", EMPTYSTRING); |
227 | 222 | table.set("thePartialUrl", EMPTYSTRING); |
228 | 223 | table.set("exists", LuaValue.TRUE); |
229 | 224 |
|
230 | | - |
231 | 225 | return table; |
232 | 226 | } |
233 | 227 | } |
0 commit comments