Search in sources :

Example 11 with TAGS

use of com.ichi2.anki.CardBrowser.Column.TAGS in project AnkiChinaAndroid by ankichinateam.

the class NoteImporter method initMapping.

public void initMapping() {
    List<String> flds = new ArrayList<>();
    JSONArray array = mModel.getJSONArray("flds");
    for (int i = 0; i < array.length(); i++) {
        flds.add(array.getJSONObject(i).getString("name"));
    }
    // truncate to provided count
    flds = flds.subList(0, Math.min(flds.size(), fields()));
    // if there's room left, add tags
    if (fields() > flds.size()) {
        flds.add("_tags");
    }
    // and if there's still room left, pad
    int iterations = fields() - flds.size();
    for (int i = 0; i < iterations; i++) {
        flds.add(null);
    }
    mMapping = flds;
}
Also used : ArrayList(java.util.ArrayList) JSONArray(com.ichi2.utils.JSONArray)

Example 12 with TAGS

use of com.ichi2.anki.CardBrowser.Column.TAGS in project AnkiChinaAndroid by ankichinateam.

the class Tags method load.

public void load(String json) {
    JSONObject tags = new JSONObject(json);
    Iterator<?> i = tags.keys();
    while (i.hasNext()) {
        String t = (String) i.next();
        mTags.put(t, tags.getInt(t));
    }
    mChanged = false;
}
Also used : JSONObject(com.ichi2.utils.JSONObject)

Example 13 with TAGS

use of com.ichi2.anki.CardBrowser.Column.TAGS in project AnkiChinaAndroid by ankichinateam.

the class Tags method flush.

public void flush() {
    if (mChanged) {
        JSONObject tags = new JSONObject();
        for (Map.Entry<String, Integer> t : mTags.entrySet()) {
            tags.put(t.getKey(), t.getValue());
        }
        ContentValues val = new ContentValues();
        val.put("tags", Utils.jsonToString(tags));
        // TODO: the database update call here sets mod = true. Verify if this is intended.
        mCol.getDb().update("col", val);
        mChanged = false;
    }
}
Also used : ContentValues(android.content.ContentValues) JSONObject(com.ichi2.utils.JSONObject) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 14 with TAGS

use of com.ichi2.anki.CardBrowser.Column.TAGS in project AnkiChinaAndroid by ankichinateam.

the class Collection method _renderQA.

public HashMap<String, String> _renderQA(long cid, Model model, long did, int ord, String tags, String[] flist, int flags, boolean browser, String qfmt, String afmt) {
    // data is [cid, nid, mid, did, ord, tags, flds, cardFlags]
    // unpack fields and create dict
    Map<String, String> fields = new HashMap<>();
    Map<String, Pair<Integer, JSONObject>> fmap = Models.fieldMap(model);
    for (String name : fmap.keySet()) {
        fields.put(name, flist[fmap.get(name).first]);
    }
    int cardNum = ord + 1;
    fields.put("Tags", tags.trim());
    fields.put("Type", model.getString("name"));
    fields.put("Deck", mDecks.name(did));
    String baseName = Decks.basename(fields.get("Deck"));
    fields.put("Subdeck", baseName);
    fields.put("CardFlag", _flagNameFromCardFlags(flags));
    JSONObject template;
    if (model.getInt("type") == Consts.MODEL_STD) {
        template = model.getJSONArray("tmpls").getJSONObject(ord);
    } else {
        template = model.getJSONArray("tmpls").getJSONObject(0);
    }
    fields.put("Card", template.getString("name"));
    fields.put(String.format(Locale.US, "c%d", cardNum), "1");
    // render q & a
    HashMap<String, String> d = new HashMap<>();
    d.put("id", Long.toString(cid));
    qfmt = TextUtils.isEmpty(qfmt) ? template.getString("qfmt") : qfmt;
    afmt = TextUtils.isEmpty(afmt) ? template.getString("afmt") : afmt;
    for (Pair<String, String> p : new Pair[] { new Pair<>("q", qfmt), new Pair<>("a", afmt) }) {
        String type = p.first;
        String format = p.second;
        if ("q".equals(type)) {
            format = fClozePatternQ.matcher(format).replaceAll(String.format(Locale.US, "{{$1cq-%d:", cardNum));
            format = fClozeTagStart.matcher(format).replaceAll(String.format(Locale.US, "<%%cq:%d:", cardNum));
        } else {
            format = fClozePatternA.matcher(format).replaceAll(String.format(Locale.US, "{{$1ca-%d:", cardNum));
            format = fClozeTagStart.matcher(format).replaceAll(String.format(Locale.US, "<%%ca:%d:", cardNum));
            // the following line differs from libanki // TODO: why?
            // fields.put("FrontSide", mMedia.stripAudio(d.get("q")));
            fields.put("FrontSide", d.get("q"));
        }
        String html = new Template(format, fields).render();
        html = ChessFilter.fenToChessboard(html, getContext());
        if (!browser) {
            // browser don't show image. So compiling LaTeX actually remove information.
            html = LaTeX.mungeQA(html, this, model);
        }
        d.put(type, html);
        // empty cloze?
        if ("q".equals(type) && model.getInt("type") == Consts.MODEL_CLOZE) {
            if (getModels()._availClozeOrds(model, flist, false).size() == 0) {
                String link = String.format("<a href=%s#cloze>%s</a>", Consts.HELP_SITE, "help");
                d.put("q", mContext.getString(R.string.empty_cloze_warning, link));
            }
        }
    }
    return d;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) HashMap(java.util.HashMap) SuppressLint(android.annotation.SuppressLint) Pair(android.util.Pair) Template(com.ichi2.libanki.template.Template)

Example 15 with TAGS

use of com.ichi2.anki.CardBrowser.Column.TAGS in project AnkiChinaAndroid by ankichinateam.

the class AnkiChinaSyncer method compareSyncTable.

private JSONObject compareSyncTable() {
    mCol = CollectionHelper.getInstance().getColSafe(AnkiDroidApp.getInstance());
    CollectionHelper.getInstance().lockCollection();
    DB db = mCol.getDb();
    db.execute("create index if not exists ix_synclog_id on synclog (id);)");
    int num = db.queryScalar("SELECT id FROM synclog");
    if (num == 0) {
        // 表是空的,则直接同步所有内容到该表
        Timber.w("no record in synclog table!");
    }
    JSONObject root = new JSONObject();
    JSONObject colJson = new JSONObject();
    JSONObject colJsonReplace = new JSONObject();
    // JSONObject decksJson = new JSONObject();
    // JSONObject modelsJson = new JSONObject();
    // JSONObject dconfJson = new JSONObject();
    colJsonReplace.put("crt", mCol.getCrt());
    colJsonReplace.put("mod", mCol.getMod());
    colJsonReplace.put("scm", mCol.getScm());
    colJsonReplace.put("ver", mCol.getVer());
    colJsonReplace.put("dty", mCol.getDirty() ? 1 : 0);
    colJsonReplace.put("usn", mCol.getUsnForSync());
    colJsonReplace.put("ls", mCol.getLs());
    colJsonReplace.put("conf", mCol.getConf());
    colJsonReplace.put("tags", mCol.getTagsJson());
    colJson.put("replace", colJsonReplace);
    root.put("col", colJson);
    root.put("decks", getChangedColJson(SYNC_LOG_TYPE_DECKS, mCol.getDecks().all()));
    root.put("dconf", getChangedColJson(SYNC_LOG_TYPE_DCONF, mCol.getDecks().allConf()));
    root.put("models", getChangedColJson(SYNC_LOG_TYPE_MODELS, mCol.getModels().all()));
    updateDialogProgress(SYNCING_DATA, "整理数据中", 6);
    root.put("cards", getChangedCardsOrNote(SYNC_LOG_TYPE_CARD));
    root.put("notes", getChangedCardsOrNote(SYNC_LOG_TYPE_NOTE));
    root.put("revlog", getAddedRevLog());
    CollectionHelper.getInstance().unlockCollection();
    // root.put("revlog", getChangedCardsOrNote(SYNC_LOG_TYPE_REVLOG));
    return root;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) DB(com.ichi2.libanki.DB) SuppressLint(android.annotation.SuppressLint)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)27 Note (com.ichi2.libanki.Note)18 JSONArray (com.ichi2.utils.JSONArray)18 ArrayList (java.util.ArrayList)13 Card (com.ichi2.libanki.Card)12 Collection (com.ichi2.libanki.Collection)12 Model (com.ichi2.libanki.Model)11 Map (java.util.Map)10 HashMap (java.util.HashMap)9 SuppressLint (android.annotation.SuppressLint)8 Resources (android.content.res.Resources)8 Deck (com.ichi2.libanki.Deck)8 JSONException (com.ichi2.utils.JSONException)8 JSONException (org.json.JSONException)8 Intent (android.content.Intent)7 View (android.view.View)7 TextView (android.widget.TextView)7 Pair (android.util.Pair)5 ContentValues (android.content.ContentValues)4 Uri (android.net.Uri)4