mirror of https://github.com/gnosygnu/xowa
Cite: Change Cite to follow MediaWiki behavior for super-scripting [#382]
parent
41deb3c0c1
commit
a94a9f0c7f
@ -0,0 +1,19 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.core.data_stores; import gplx.*; import gplx.core.*;
|
||||
public interface Gfo_data_itm {
|
||||
String Key();
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.core.data_stores; import gplx.*; import gplx.core.*;
|
||||
public class Gfo_data_store {
|
||||
private final Hash_adp hash = Hash_adp_.New();
|
||||
public Gfo_data_itm Get_or_null(String key) {
|
||||
return (Gfo_data_itm)hash.Get_by(key);
|
||||
}
|
||||
public void Set(Gfo_data_itm itm) {
|
||||
hash.Add_if_dupe_use_nth(itm.Key(), itm);
|
||||
}
|
||||
public void Clear() {
|
||||
hash.Clear();
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.htmls; import gplx.*; import gplx.langs.*;
|
||||
public class Gfh_atr_itm {
|
||||
public Gfh_atr_itm(byte[] key, byte[] val) {
|
||||
this.key = key;
|
||||
this.val = val;
|
||||
}
|
||||
public byte[] Key() {return key;} private final byte[] key;
|
||||
public byte[] Val() {return val;} private final byte[] val;
|
||||
public static Gfh_atr_itm New(byte[] key, String val) {return new Gfh_atr_itm(key, Bry_.new_u8(val));}
|
||||
public static Gfh_atr_itm New(byte[] key, byte[] val) {return new Gfh_atr_itm(key, val);}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.langs.htmls; import gplx.*; import gplx.langs.*;
|
||||
public class Gfh_html_ {
|
||||
public static byte[] rawElement(Bry_bfr bfr, int tag_id, byte[] body, Gfh_atr_itm... atrs) {
|
||||
// add "<tag"
|
||||
byte[] tag_name = Bry_.new_u8(Gfh_tag_.To_str(tag_id));
|
||||
Gfh_tag_.Bld_lhs_bgn(bfr, tag_name);
|
||||
|
||||
// add " key1=val1 ..."
|
||||
int len = atrs.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Gfh_atr_itm atr = atrs[i];
|
||||
Gfh_atr_.Add(bfr, atr.Key(), atr.Val());
|
||||
}
|
||||
|
||||
// add ">"
|
||||
Gfh_tag_.Bld_lhs_end_nde(bfr);
|
||||
|
||||
bfr.Add(body);
|
||||
|
||||
// add "</tag>"
|
||||
Gfh_tag_.Bld_rhs(bfr, tag_name);
|
||||
return bfr.To_bry_and_clear();
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.xtns.cites; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
class Cite_link_label_mgr {
|
||||
private final Hash_adp_bry hash = Hash_adp_bry.cs();
|
||||
public void Clear() {
|
||||
hash.Clear();
|
||||
}
|
||||
public Cite_link_label_grp Get_or_null(byte[] group) {
|
||||
return (Cite_link_label_grp)hash.Get_by(group);
|
||||
}
|
||||
public void Add(byte[] group, Cite_link_label_grp grp) {
|
||||
hash.Add(group, grp);
|
||||
}
|
||||
}
|
||||
class Cite_link_label_grp {
|
||||
private final byte[][] labels;
|
||||
public Cite_link_label_grp(byte[] name, byte[][] labels) {
|
||||
this.name = name;
|
||||
this.labels = labels;
|
||||
}
|
||||
public byte[] Name() {return name;} private final byte[] name;
|
||||
public int Len() {return labels.length;}
|
||||
public byte[] Get_or_null(int idx) {
|
||||
return idx < labels.length ? labels[idx] : null;
|
||||
}
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.xtns.cites; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import gplx.core.brys.fmtrs.*;
|
||||
import gplx.xowa.langs.*; import gplx.xowa.langs.msgs.*;
|
||||
import gplx.langs.htmls.*;
|
||||
class Cite_mgr { // REF.MW:/extensions/Cite/includes/Cite.php
|
||||
private final Xowe_wiki wiki;
|
||||
private final Hash_adp_bry messages_by_group = Hash_adp_bry.cs();
|
||||
private Cite_xtn_data xtn_data;
|
||||
private Cite_link_label_mgr link_label_mgr;
|
||||
public Cite_mgr(Xowe_wiki wiki) {
|
||||
this.wiki = wiki;
|
||||
}
|
||||
public byte[] getLinkLabel(int offset, byte[] group) {
|
||||
// get xtn_data; NOTE: should go in Init_by_wiki, but Init_by_wiki doesn't get called by tests
|
||||
if (xtn_data == null) {
|
||||
this.xtn_data = Cite_xtn_data.Get_or_make(wiki.Parser_mgr().Data_store());
|
||||
this.link_label_mgr = xtn_data.Link_labels();
|
||||
}
|
||||
|
||||
// get message; use cache to avoid multiple concantenations; EX: "cite_link_label_group-" + "lower-roman";
|
||||
byte[] message = (byte[])messages_by_group.Get_by_bry(group);
|
||||
if (message == null) {
|
||||
message = Bry_.Add(Msg__cite_link_label_group, group);
|
||||
messages_by_group.Add(group, message);
|
||||
}
|
||||
|
||||
// get linkLabels or gen
|
||||
Cite_link_label_grp linkLabels = link_label_mgr.Get_or_null(group);
|
||||
if (linkLabels == null) {
|
||||
linkLabels = this.genLinkLabels(group, message);
|
||||
}
|
||||
|
||||
// if linkLabels group missing, just concat; EX: "custom-group 1"
|
||||
if (linkLabels.Len() == 0) {
|
||||
return Bry_.Add((group.length == 0 ? Bry_.Empty : Bry_.Add(group, Byte_ascii.Space)), wiki.Lang().Num_mgr().Format_num(offset));
|
||||
}
|
||||
|
||||
// linkLabels group exists; pull corresponding offset; EX: "5" in lower-roman -> "v"
|
||||
byte[] rv = linkLabels.Get_or_null(offset - 1);
|
||||
return rv == null
|
||||
? this.plainError(Msg__cite_error_no_link_label_group, group, message)
|
||||
: rv;
|
||||
}
|
||||
private Cite_link_label_grp genLinkLabels(byte[] group, byte[] message) {
|
||||
Xol_msg_itm msg = wiki.Msg_mgr().Find_or_null(message);
|
||||
byte[][] text = msg == null
|
||||
? Bry_.Ary_empty
|
||||
: Bry_split_.Split_ws(msg.Val());
|
||||
|
||||
Cite_link_label_grp grp = new Cite_link_label_grp(group, text);
|
||||
link_label_mgr.Add(group, grp);
|
||||
return grp;
|
||||
}
|
||||
private byte[] plainError(byte[] key, Object... ary) {
|
||||
Bry_bfr tmp_bfr = Bry_bfr_.New();
|
||||
Bry_fmtr tmp_fmtr = Bry_fmtr.New__tmp();
|
||||
|
||||
// build specific error msg
|
||||
Xol_msg_itm msg_itm = wiki.Msg_mgr().Find_or_null(key);
|
||||
byte[] msg = (msg_itm == null) ? key : msg_itm.Fmt(tmp_bfr, tmp_fmtr, ary);
|
||||
|
||||
// wrap specific error message with "Cite error: "
|
||||
msg_itm = wiki.Msg_mgr().Find_or_null(Msg__cite_error);
|
||||
if (msg_itm != null)
|
||||
msg = msg_itm.Fmt(tmp_bfr, tmp_fmtr, msg);
|
||||
|
||||
// generate tag
|
||||
Xol_lang_itm lang = wiki.Lang();
|
||||
byte[] ret = Gfh_html_.rawElement
|
||||
( tmp_bfr, Gfh_tag_.Id__span
|
||||
, msg
|
||||
, Gfh_atr_itm.New(Gfh_atr_.Bry__class, "error mw-ext-cite-error")
|
||||
, Gfh_atr_itm.New(Gfh_atr_.Bry__lang , lang.Key_bry())
|
||||
, Gfh_atr_itm.New(Gfh_atr_.Bry__dir , lang.Dir_ltr_bry())
|
||||
);
|
||||
return ret;
|
||||
}
|
||||
public static final byte[]
|
||||
Msg__cite_link_label_group = Bry_.new_a7("cite_link_label_group-")
|
||||
, Msg__cite_error = Bry_.new_a7("cite_error")
|
||||
, Msg__cite_error_no_link_label_group = Bry_.new_a7("cite_error_no_link_label_group")
|
||||
;
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.xtns.cites; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import org.junit.*; import gplx.core.tests.*;
|
||||
import gplx.xowa.langs.msgs.*;
|
||||
public class Cite_mgr_tst {
|
||||
private final Cite_mgr_fxt fxt = new Cite_mgr_fxt();
|
||||
@Test public void getLinkLabel_lower_alpha() {
|
||||
fxt.Test__getLinkLabel("lower-alpha", 1, "a");
|
||||
fxt.Test__getLinkLabel("lower-alpha", 2, "b");
|
||||
fxt.Test__getLinkLabel("lower-alpha", 3, "c");
|
||||
}
|
||||
@Test public void getLinkLabel_upper_roman() {
|
||||
fxt.Test__getLinkLabel("upper-roman", 1, "I");
|
||||
fxt.Test__getLinkLabel("upper-roman", 2, "II");
|
||||
fxt.Test__getLinkLabel("upper-roman", 3, "III");
|
||||
}
|
||||
@Test public void getLinkLabel_unknown() {
|
||||
fxt.Test__getLinkLabel("unknown", 1, "unknown 1");
|
||||
}
|
||||
@Test public void getLinkLabel_err() {
|
||||
fxt.Test__getLinkLabel("upper-roman", 4, "<span class=\"error mw-ext-cite-error\" lang=\"en\" dir=\"ltr\">Cite error: Ran out of custom link labels for group \"upper-roman\".\nDefine more in the <nowiki>[[MediaWiki:cite_link_label_group-upper-roman]]</nowiki> message.</span>");
|
||||
}
|
||||
}
|
||||
class Cite_mgr_fxt {
|
||||
private Cite_mgr mgr;
|
||||
private Xowe_wiki wiki;
|
||||
public Cite_mgr_fxt() {
|
||||
// init cite_mgr
|
||||
Xoae_app app = Xoa_app_fxt.Make__app__edit();
|
||||
this.wiki = Xoa_app_fxt.Make__wiki__edit(app);
|
||||
this.mgr = new Cite_mgr(wiki);
|
||||
|
||||
// init common messages
|
||||
this.Init__msg__label_grp("lower-alpha", "a b c");
|
||||
this.Init__msg__label_grp("upper-roman", "I II III");
|
||||
this.Init__msg(Cite_mgr.Msg__cite_error, "Cite error: ~{0}");
|
||||
this.Init__msg(Cite_mgr.Msg__cite_error_no_link_label_group, "Ran out of custom link labels for group \"~{0}\".\nDefine more in the <nowiki>[[MediaWiki:~{1}]]</nowiki> message.");
|
||||
}
|
||||
private void Init__msg__label_grp(String key, String val) {
|
||||
this.Init__msg(String_.new_u8(Cite_mgr.Msg__cite_link_label_group) + key, val);
|
||||
}
|
||||
private void Init__msg(String key, String val) {Init__msg(Bry_.new_u8(key), val);}
|
||||
private void Init__msg(byte[] key, String val) {
|
||||
Xol_msg_itm msg = wiki.Msg_mgr().Get_or_make(key);
|
||||
msg.Atrs_set(Bry_.new_u8(val), false, false);
|
||||
}
|
||||
public void Test__getLinkLabel(String group, int offset, String expd) {
|
||||
Gftest.Eq__str(expd, mgr.getLinkLabel(offset, Bry_.new_u8(group)));
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012-2017 gnosygnu@gmail.com
|
||||
|
||||
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
|
||||
or alternatively under the terms of the Apache License Version 2.0.
|
||||
|
||||
You may use XOWA according to either of these licenses as is most appropriate
|
||||
for your project on a case-by-case basis.
|
||||
|
||||
The terms of each license can be found in the source code repository:
|
||||
|
||||
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
|
||||
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
|
||||
*/
|
||||
package gplx.xowa.xtns.cites; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import gplx.core.data_stores.*;
|
||||
class Cite_xtn_data implements Gfo_data_itm {
|
||||
public String Key() {return KEY;}
|
||||
public void Clear() {
|
||||
link_labels.Clear();
|
||||
}
|
||||
public Cite_link_label_mgr Link_labels() {return link_labels;} private final Cite_link_label_mgr link_labels = new Cite_link_label_mgr();
|
||||
|
||||
private static final String KEY = "xtn.Cite";
|
||||
public static Cite_xtn_data Get_or_make(Gfo_data_store data_store) {
|
||||
Cite_xtn_data rv = (Cite_xtn_data)data_store.Get_or_null(KEY);
|
||||
if (rv == null) {
|
||||
rv = new Cite_xtn_data();
|
||||
data_store.Set(rv);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue