01 package org.qedeq.kernel.se.visitor;
02
03 import org.qedeq.base.utility.StringUtility;
04 import org.qedeq.kernel.se.base.module.Latex;
05 import org.qedeq.kernel.se.base.module.LatexList;
06
07 /**
08 * Transform latex list into text. We make here only a basic conversion to have a plain text
09 * description of such things as chapter titles. So we have to remove something like
10 * "\index".
11 * <br/>
12 * TODO 20130126 m31: this transformation is mainly used to get a good location description
13 * when a plugin is running. So it must work with chapter, section and subsection titles.
14 * We just have to check what LaTeX stuff is used there.
15 * <br/>
16 * Another problem: currently only the method {@link #transform(LatexList)} is called.
17 * <br/>
18 * It might be a good idea to idea to externalize the usage. So one could use a better transformer
19 * transformer (like Latex2UnicodeParser) if we have it in our class path...
20 *
21 * @author Michael Meyling
22 */
23 public class LatexList2Text {
24
25 /**
26 * Filters English entry out of LaTeX list.
27 *
28 * @param list List of LaTeX entries.
29 * @return Selected entry transformed into text.
30 */
31 public String transform(final LatexList list) {
32 return transform(list, "en");
33 }
34
35 /**
36 * Filters given language entry out of LaTeX list.
37 * Fallback is the default language.
38 *
39 * @param list List of LaTeX entries.
40 * @param language Filter for this language.
41 * @return Selected entry transformed into text.
42 */
43 public String transform(final LatexList list, final String language) {
44 if (list == null) {
45 return "";
46 }
47 // if we got no language we take "en"
48 String lan = (language != null ? language : "en");
49 for (int i = 0; i < list.size(); i++) {
50 if (list.get(i) != null && lan.equals(list.get(i).getLanguage())) {
51 return getLatex(list.get(i));
52 }
53 }
54 // assume entry with missing language as default
55 for (int i = 0; i < list.size(); i++) {
56 if (list.get(i) != null && list.get(i).getLanguage() == null) {
57 return getLatex(list.get(i));
58 }
59 }
60 // if we haven't tried "en" yet we give it a try
61 if (!"en".equals(lan)) {
62 lan = "en";
63 for (int i = 0; i < list.size(); i++) {
64 if (list.get(i) != null && lan.equals(list.get(i).getLanguage())) {
65 return getLatex(list.get(i));
66 }
67 }
68 }
69 // fallback: now we take the first entry
70 for (int i = 0; i < list.size(); i++) {
71 if (list.get(i) != null) {
72 return getLatex(list.get(i));
73 }
74 }
75 // nothing found, so we return just an empty string
76 return "";
77 }
78
79 protected String getLatex(final Latex latex) {
80 if (latex == null) {
81 return "";
82 }
83 String result = latex.getLatex();
84 if (result == null) {
85 result = "";
86 }
87 result = result.trim();
88 result = result.replaceAll("\\\\index\\{.*\\}", "");
89 result = result.replaceAll("\\\\(\\w*)\\{(.*)\\}", "$2");
90 result = StringUtility.replace(result, "{", "");
91 result = StringUtility.replace(result, "}", "");
92 return result.trim();
93 }
94
95 }
|