Search in sources :

Example 1 with MustacheException

use of com.samskivert.mustache.MustacheException in project Anki-Android by Ramblurr.

the class Collection method _renderQA.

public HashMap<String, String> _renderQA(Object[] data, List<String> args) {
    // data is [cid, nid, mid, did, ord, tags, flds]
    // unpack fields and create dict
    String[] flist = Utils.splitFields((String) data[6]);
    Map<String, String> fields = new HashMap<String, String>();
    long modelId = (Long) data[2];
    JSONObject model = mModels.get(modelId);
    Map<String, Pair<Integer, JSONObject>> fmap = mModels.fieldMap(model);
    for (String fname : fmap.keySet()) {
        fields.put(fname, flist[fmap.get(fname).first]);
    }
    fields.put("Tags", ((String) data[5]).trim());
    try {
        fields.put("Type", (String) model.get("name"));
        fields.put("Deck", mDecks.name((Long) data[3]));
        JSONObject template;
        if (model.getInt("type") == Sched.MODEL_STD) {
            template = model.getJSONArray("tmpls").getJSONObject((Integer) data[4]);
        } else {
            template = model.getJSONArray("tmpls").getJSONObject(0);
        }
        fields.put("Card", template.getString("name"));
        fields.put("c" + (((Integer) data[4]) + 1), "1");
        // render q & a
        HashMap<String, String> d = new HashMap<String, String>();
        try {
            d.put("id", Long.toString((Long) data[0]));
            String qfmt = template.getString("qfmt");
            String afmt = template.getString("afmt");
            String html;
            String format;
            // runFilter mungeFields for type "q"
            Models.fieldParser fparser = new Models.fieldParser(fields);
            Matcher m = fClozePattern.matcher(qfmt);
            format = m.replaceFirst(String.format(Locale.US, "{{cq:%d:", ((Integer) data[4]) + 1));
            m = fAltClozePattern.matcher(format);
            format = m.replaceFirst(String.format(Locale.US, "<%%cq:%d:", ((Integer) data[4]) + 1));
            html = mModels.getCmpldTemplate(format).execute(fparser);
            html = (String) AnkiDroidApp.getHooks().runFilter("mungeQA", html, "q", fields, model, data, this);
            d.put("q", html);
            // empty cloze?
            if (model.getInt("type") == Sched.MODEL_CLOZE) {
                if (getModels()._availClozeOrds(model, (String) data[6], false).size() == 0) {
                    d.put("q", "Please edit this note and add some cloze deletions.");
                }
            }
            fields.put("FrontSide", d.get("q"));
            // runFilter mungeFields for type "a"
            fparser = new Models.fieldParser(fields);
            m = fClozePattern.matcher(afmt);
            format = m.replaceFirst(String.format(Locale.US, "{{ca:%d:", ((Integer) data[4]) + 1));
            m = fAltClozePattern.matcher(format);
            format = m.replaceFirst(String.format(Locale.US, "<%%ca:%d:", ((Integer) data[4]) + 1));
            html = mModels.getCmpldTemplate(format).execute(fparser);
            html = (String) AnkiDroidApp.getHooks().runFilter("mungeQA", html, "a", fields, model, data, this);
            d.put("a", html);
            // empty cloze?
            if (model.getInt("type") == Sched.MODEL_CLOZE) {
                if (getModels()._availClozeOrds(model, (String) data[6], false).size() == 0) {
                    d.put("q", AnkiDroidApp.getAppResources().getString(com.ichi2.anki.R.string.empty_cloze_warning, "<a href=" + HELP_SITE + "#cloze>" + AnkiDroidApp.getAppResources().getString(com.ichi2.anki.R.string.help_cloze) + "</a>"));
                }
            }
        } catch (MustacheException e) {
            Resources res = AnkiDroidApp.getAppResources();
            String templateError = String.format(TEMPLATE_ERROR, res.getString(R.string.template_error), res.getString(R.string.template_error_detail), e.getMessage(), res.getString(R.string.note_type), model.getString("name"), res.getString(R.string.card_type), template.getString("name"), res.getString(R.string.template_error_fix));
            d.put("q", templateError);
            d.put("a", templateError);
        }
        return d;
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}
Also used : HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) MustacheException(com.samskivert.mustache.MustacheException) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) Resources(android.content.res.Resources) Pair(com.ichi2.anki.Pair)

Example 2 with MustacheException

use of com.samskivert.mustache.MustacheException in project killbill by killbill.

the class OverdueStateApplicator method sendEmailIfRequired.

private void sendEmailIfRequired(final UUID accountId, final BillingState billingState, final OverdueState nextOverdueState, final InternalTenantContext context) throws AccountApiException {
    // If sending is not configured, skip
    if (nextOverdueState.getEmailNotification() == null) {
        return;
    }
    final Account account = accountApi.getAccountById(accountId, context);
    if (Strings.emptyToNull(account.getEmail()) == null) {
        log.warn("Unable to send overdue notification email for account {} and overdueable {}: no email specified", account.getId(), account.getId());
        return;
    }
    final List<String> to = ImmutableList.<String>of(account.getEmail());
    // TODO - should we look at the account CC: list?
    final List<String> cc = ImmutableList.<String>of();
    final String subject = nextOverdueState.getEmailNotification().getSubject();
    try {
        // Generate and send the email
        final String emailBody = overdueEmailGenerator.generateEmail(account, billingState, account, nextOverdueState);
        if (nextOverdueState.getEmailNotification().isHTML()) {
            emailSender.sendHTMLEmail(to, cc, subject, emailBody);
        } else {
            emailSender.sendPlainTextEmail(to, cc, subject, emailBody);
        }
    } catch (final IOException e) {
        log.warn("Unable to generate or send overdue notification email for accountId='{}'", account.getId(), e);
    } catch (final EmailApiException e) {
        log.warn("Unable to send overdue notification email for accountId='{}'", account.getId(), e);
    } catch (final MustacheException e) {
        log.warn("Unable to generate overdue notification email for accountId='{}'", account.getId(), e);
    }
}
Also used : Account(org.killbill.billing.account.api.Account) EmailApiException(org.killbill.billing.util.email.EmailApiException) MustacheException(com.samskivert.mustache.MustacheException) IOException(java.io.IOException)

Aggregations

MustacheException (com.samskivert.mustache.MustacheException)2 Resources (android.content.res.Resources)1 Pair (com.ichi2.anki.Pair)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Matcher (java.util.regex.Matcher)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 Account (org.killbill.billing.account.api.Account)1 EmailApiException (org.killbill.billing.util.email.EmailApiException)1