+
+ final StringBuilder foreignBuilder = new StringBuilder();
+ final List<EnForeignParser.ListSection> listSections = new ArrayList<>();
+
+ appendAndIndexWikiCallback.reset(foreignBuilder, null);
+ this.state = State.ENGLISH_DEF_OF_FOREIGN; // TODO: this is wrong, need new category....
+ titleAppended = false;
+ wordForms.clear();
+
+ try {
+
+ EnForeignParser.ListSection lastListSection = null;
+
+ int currentHeadingDepth = posDepth;
+ while (wikiTokenizer.nextToken() != null) {
+ if (wikiTokenizer.isHeading()) {
+ currentHeadingDepth = wikiTokenizer.headingDepth();
+
+ if (currentHeadingDepth <= posDepth) {
+ wikiTokenizer.returnToLineStart();
+ return;
+ }
+ } // heading
+
+ if (currentHeadingDepth > posDepth) {
+ // TODO: deal with other neat info sections inside POS
+ continue;
+ }
+
+ if (wikiTokenizer.isFunction()) {
+ final String name = wikiTokenizer.functionName();
+ final List<String> args = wikiTokenizer.functionPositionArgs();
+ final Map<String,String> namedArgs = wikiTokenizer.functionNamedArgs();
+ // First line is generally a repeat of the title with some extra information.
+ // We need to build up the left side (foreign text, tokens) separately from the
+ // right side (English). The left-side may get paired with multiple right sides.
+ // The left side should get filed under every form of the word in question (singular, plural).
+
+ // For verbs, the conjugation comes later on in a deeper section.
+ // Ideally, we'd want to file every English entry with the verb
+ // under every verb form coming from the conjugation.
+ // Ie. under "fa": see: "make :: fare" and "do :: fare"
+ // But then where should we put the conjugation table?
+ // I think just under fare. But then we need a way to link to the entry (actually the row, since entries doesn't show up!)
+ // for the conjugation table from "fa".
+ // Would like to be able to link to a lang#token.
+
+
+ String head = namedArgs.remove("head");
+ final String tr = namedArgs.remove("tr");
+ if (head == null && tr != null && !titleAppended) {
+ head = title;
+ }
+ if (head != null) {
+ final String form = appendAndIndexWikiCallback.dispatch(head, EntryTypeName.WIKTIONARY_TITLE_MULTI);
+ wordForms.add(form);
+ appendAndIndexWikiCallback.builder.append(" ");
+ titleAppended = true;
+ }
+ if (tr != null) {
+ appendAndIndexWikiCallback.builder.append(" (");
+ final String form = appendAndIndexWikiCallback.dispatch(tr, EntryTypeName.WIKTIONARY_TRANSLITERATION);
+ wordForms.add(form);
+ appendAndIndexWikiCallback.builder.append(") ");
+ }
+
+ appendAndIndexWikiCallback.onFunction(wikiTokenizer, name, args, namedArgs);
+
+ } else if (wikiTokenizer.isListItem()) {
+ final String prefix = wikiTokenizer.listItemPrefix();
+ if (lastListSection != null &&
+ prefix.startsWith(lastListSection.firstPrefix) &&
+ prefix.length() > lastListSection.firstPrefix.length()) {
+ lastListSection.nextPrefixes.add(prefix);
+ lastListSection.nextLines.add(wikiTokenizer.listItemWikiText());
+ } else {
+ lastListSection = new ListSection(prefix, wikiTokenizer.listItemWikiText());
+ listSections.add(lastListSection);
+ }
+ } else if (lastListSection != null) {
+ // Don't append anything after the lists, because there's crap.
+ } else if (wikiTokenizer.isWikiLink()) {
+ // Unindexed!
+ foreignBuilder.append(wikiTokenizer.wikiLinkText());
+
+ } else if (wikiTokenizer.isPlainText()) {
+ // Unindexed!
+ foreignBuilder.append(wikiTokenizer.token());
+ } else if (wikiTokenizer.isHtml()) {
+ if (!wikiTokenizer.token().startsWith("<ref>")) {
+ foreignBuilder.append(wikiTokenizer.token());
+ }
+ } else if (wikiTokenizer.isMarkup() ||
+ wikiTokenizer.isNewline() ||
+ wikiTokenizer.isComment()) {
+ // Do nothing.
+ } else {
+ LOG.warning("Unexpected token: " + wikiTokenizer.token());
+ assert !wikiTokenizer.errors().isEmpty();
+ }
+ }
+
+ } finally {
+ // Here's where we exit.
+ // Should we make an entry even if there are no foreign list items?
+ String foreign = foreignBuilder.toString().trim();
+ if (!titleAppended && !foreign.toLowerCase().startsWith(title.toLowerCase())) {
+ foreign = String.format("%s %s", title, foreign);
+ }
+ if (!langPattern.matcher(lang).matches()) {
+ foreign = String.format("(%s) %s", lang, foreign);
+ }
+ for (final EnForeignParser.ListSection listSection : listSections) {
+ doForeignListSection(foreign, title, wordForms, listSection);
+ }