- protected InputStream openInput(URL source) throws IOException {
- return Instance.getCache().open(source, this, false);
- }
-
- /**
- * Reset the given {@link InputStream} and return it.
- *
- * @param in
- * the {@link InputStream} to reset
- *
- * @return the same {@link InputStream} after reset
- */
- protected InputStream reset(InputStream in) {
- try {
- in.reset();
- } catch (IOException e) {
- }
- return in;
- }
-
- /**
- * Reset then return {@link BasicSupport#in}.
- *
- * @return {@link BasicSupport#in}
- */
- protected InputStream getInput() {
- return reset(in);
- }
-
- /**
- * Fix the author name if it is prefixed with some "by" {@link String}.
- *
- * @param author
- * the author with a possible prefix
- *
- * @return the author without prefixes
- */
- protected String fixAuthor(String author) {
- if (author != null) {
- for (String suffix : new String[] { " ", ":" }) {
- for (String byString : Instance.getConfig()
- .getString(Config.BYS).split(",")) {
- byString += suffix;
- if (author.toUpperCase().startsWith(byString.toUpperCase())) {
- author = author.substring(byString.length()).trim();
- }
- }
- }
-
- // Special case (without suffix):
- if (author.startsWith("©")) {
- author = author.substring(1);
- }
- }
-
- return author;
- }
-
- /**
- * Check quotes for bad format (i.e., quotes with normal paragraphs inside)
- * and requotify them (i.e., separate them into QUOTE paragraphs and other
- * paragraphs (quotes or not)).
- *
- * @param para
- * the paragraph to requotify (not necessarily a quote)
- *
- * @return the correctly (or so we hope) quotified paragraphs
- */
- protected List<Paragraph> requotify(Paragraph para) {
- List<Paragraph> newParas = new ArrayList<Paragraph>();
-
- if (para.getType() == ParagraphType.QUOTE
- && para.getContent().length() > 2) {
- String line = para.getContent();
- boolean singleQ = line.startsWith("" + openQuote);
- boolean doubleQ = line.startsWith("" + openDoubleQuote);
-
- // Do not try when more than one quote at a time
- // (some stories are not easily readable if we do)
- if (singleQ
- && line.indexOf(closeQuote, 1) < line
- .lastIndexOf(closeQuote)) {
- newParas.add(para);
- return newParas;
- }
- if (doubleQ
- && line.indexOf(closeDoubleQuote, 1) < line
- .lastIndexOf(closeDoubleQuote)) {
- newParas.add(para);
- return newParas;
- }
- //
-
- if (!singleQ && !doubleQ) {
- line = openDoubleQuote + line + closeDoubleQuote;
- newParas.add(new Paragraph(ParagraphType.QUOTE, line, para
- .getWords()));
- } else {
- char open = singleQ ? openQuote : openDoubleQuote;
- char close = singleQ ? closeQuote : closeDoubleQuote;
-
- int posDot = -1;
- boolean inQuote = false;
- int i = 0;
- for (char car : line.toCharArray()) {
- if (car == open) {
- inQuote = true;
- } else if (car == close) {
- inQuote = false;
- } else if (car == '.' && !inQuote) {
- posDot = i;
- break;
- }
- i++;
- }
-
- if (posDot >= 0) {
- String rest = line.substring(posDot + 1).trim();
- line = line.substring(0, posDot + 1).trim();
- long words = 1;
- for (char car : line.toCharArray()) {
- if (car == ' ') {
- words++;
- }
- }
- newParas.add(new Paragraph(ParagraphType.QUOTE, line, words));
- if (!rest.isEmpty()) {
- newParas.addAll(requotify(processPara(rest)));
- }
- } else {
- newParas.add(para);
- }
- }