Search in sources :

Example 1 with RustCleanup

use of net.ankiweb.rsdroid.RustCleanup in project Anki-Android by ankidroid.

the class Collection method render_output_legacy.

@NonNull
@RustCleanup("Hack for Card Template Previewer, needs review")
public TemplateRenderOutput render_output_legacy(@NonNull Card c, boolean reload, boolean browser) {
    Note f = c.note(reload);
    Model m = c.model();
    JSONObject t = c.template();
    long did;
    if (c.isInDynamicDeck()) {
        did = c.getODid();
    } else {
        did = c.getDid();
    }
    HashMap<String, String> qa;
    if (browser) {
        String bqfmt = t.getString("bqfmt");
        String bafmt = t.getString("bafmt");
        qa = _renderQA(c.getId(), m, did, c.getOrd(), f.stringTags(), f.getFields(), c.internalGetFlags(), browser, bqfmt, bafmt);
    } else {
        qa = _renderQA(c.getId(), m, did, c.getOrd(), f.stringTags(), f.getFields(), c.internalGetFlags());
    }
    return new TemplateRenderOutput(qa.get("q"), qa.get("a"), null, null, c.model().getString("css"));
}
Also used : JSONObject(com.ichi2.utils.JSONObject) TemplateRenderOutput(com.ichi2.libanki.TemplateManager.TemplateRenderContext.TemplateRenderOutput) RustCleanup(net.ankiweb.rsdroid.RustCleanup) NonNull(androidx.annotation.NonNull)

Example 2 with RustCleanup

use of net.ankiweb.rsdroid.RustCleanup in project Anki-Android by ankidroid.

the class Collection method _renderQA.

@RustCleanup("#8951 - Remove FrontSide added to the front")
@NonNull
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, Pair<Integer, JSONObject>> fmap = Models.fieldMap(model);
    Set<Map.Entry<String, Pair<Integer, JSONObject>>> maps = fmap.entrySet();
    Map<String, String> fields = HashUtil.HashMapInit(maps.size() + 8);
    for (Map.Entry<String, Pair<Integer, JSONObject>> entry : maps) {
        fields.put(entry.getKey(), flist[entry.getValue().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.isStd()) {
        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 = HashUtil.HashMapInit(2);
    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));
            fields.put("FrontSide", "");
        } 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;
        try {
            html = ParsedNode.parse_inner(format).render(fields, "q".equals(type), getContext());
        } catch (TemplateError er) {
            Timber.w(er);
            html = er.message(getContext());
        }
        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.isCloze()) {
            if (Models._availClozeOrds(model, flist, false).isEmpty()) {
                String link = String.format("<a href=\"%s\">%s</a>", mContext.getResources().getString(R.string.link_ankiweb_docs_cloze_deletion), "help");
                System.out.println(link);
                d.put("q", mContext.getString(R.string.empty_cloze_warning, link));
            }
        }
    }
    return d;
}
Also used : SuppressLint(android.annotation.SuppressLint) JSONObject(com.ichi2.utils.JSONObject) TemplateError(com.ichi2.libanki.template.TemplateError) Map(java.util.Map) HashMap(java.util.HashMap) Pair(android.util.Pair) RustCleanup(net.ankiweb.rsdroid.RustCleanup) NonNull(androidx.annotation.NonNull)

Example 3 with RustCleanup

use of net.ankiweb.rsdroid.RustCleanup in project Anki-Android by ankidroid.

the class SchedV2 method _updateCutoff.

/**
 * Daily cutoff ************************************************************* **********************************
 * This function uses GregorianCalendar so as to be sensitive to leap years, daylight savings, etc.
 */
/* Overriden: other way to count time*/
@RustCleanup("remove timing == null check once JavaBackend is removed")
public void _updateCutoff() {
    int oldToday = mToday == null ? 0 : mToday;
    SchedTimingToday timing = _timingToday();
    if (timing == null) {
        mToday = _daysSinceCreation();
        mDayCutoff = _dayCutoff();
    } else if (_new_timezone_enabled()) {
        mToday = timing.days_elapsed();
        mDayCutoff = timing.next_day_at();
    } else {
        mToday = _daysSinceCreation();
        mDayCutoff = _dayCutoff();
    }
    if (oldToday != mToday) {
        mCol.log(mToday, mDayCutoff);
    }
    // instead
    for (Deck deck : mCol.getDecks().all()) {
        update(deck);
    }
    // unbury if the day has rolled over
    int unburied = mCol.get_config("lastUnburied", 0);
    if (unburied < mToday) {
        SyncStatus.ignoreDatabaseModification(this::unburyCards);
        mCol.set_config("lastUnburied", mToday);
    }
}
Also used : SchedTimingToday(com.ichi2.libanki.backend.model.SchedTimingToday) Deck(com.ichi2.libanki.Deck) RustCleanup(net.ankiweb.rsdroid.RustCleanup)

Aggregations

RustCleanup (net.ankiweb.rsdroid.RustCleanup)3 NonNull (androidx.annotation.NonNull)2 JSONObject (com.ichi2.utils.JSONObject)2 SuppressLint (android.annotation.SuppressLint)1 Pair (android.util.Pair)1 Deck (com.ichi2.libanki.Deck)1 TemplateRenderOutput (com.ichi2.libanki.TemplateManager.TemplateRenderContext.TemplateRenderOutput)1 SchedTimingToday (com.ichi2.libanki.backend.model.SchedTimingToday)1 TemplateError (com.ichi2.libanki.template.TemplateError)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1