Exemple #1
0
 private String linkHtml(
     ParserInput parserInput,
     ParserOutput parserOutput,
     int mode,
     String link,
     String text,
     String punctuation,
     boolean numberedCaption)
     throws ParserException {
   if (link.toLowerCase().startsWith("mailto://")) {
     // fix bad mailto syntax
     link = "mailto:" + link.substring("mailto://".length());
   }
   String caption = link;
   if (!StringUtils.isBlank(text)) {
     // pass a parameter via the parserInput to prevent nested links from being generated
     parserInput.getTempParams().put(HTML_LINK_CAPTION, true);
     caption = JFlexParserUtil.parseFragment(parserInput, parserOutput, text, mode);
     parserInput.getTempParams().remove(HTML_LINK_CAPTION);
   } else if (numberedCaption) {
     // set the caption of the form "[1]"
     int counter = 1;
     if (parserInput.getTempParams().get(HTML_LINK_CAPTION_COUNTER) != null) {
       counter = (Integer) parserInput.getTempParams().get(HTML_LINK_CAPTION_COUNTER);
     }
     parserInput.getTempParams().put(HTML_LINK_CAPTION_COUNTER, counter + 1);
     caption = "[" + counter + "]";
   }
   return LinkUtil.buildExternalLinkHtml(link, "externallink", caption) + punctuation;
 }
Exemple #2
0
 @Override
 public void appendSignature(Appendable writer, int numberOfTildes) throws IOException {
   switch (numberOfTildes) {
     case 3:
       writer.append(
           JFlexParserUtil.parseFragment(
               fParserInput, fParserOutput, "~~~", JFlexParser.MODE_MINIMAL));
       break;
     case 4:
       writer.append(
           JFlexParserUtil.parseFragment(
               fParserInput, fParserOutput, "~~~~", JFlexParser.MODE_MINIMAL));
       break;
     case 5:
       writer.append(
           JFlexParserUtil.parseFragment(
               fParserInput, fParserOutput, "~~~~~", JFlexParser.MODE_MINIMAL));
       break;
   }
 }
Exemple #3
0
 /**
  * Parse a raw Wiki link of the form "[[link|text]]", and return a WikiLink object representing
  * the link.
  *
  * @param parserInput Input configuration settings for this parser instance.
  * @param raw The raw Wiki link text.
  * @return A WikiLink object that represents the link.
  */
 protected static WikiLink parseWikiLink(
     ParserInput parserInput, ParserOutput parserOutput, String raw) throws ParserException {
   if (StringUtils.isBlank(raw)) {
     return new WikiLink();
   }
   raw = raw.trim();
   String suffix = ((!raw.endsWith("]]")) ? raw.substring(raw.lastIndexOf("]]") + 2) : null);
   // for performance reasons use String methods rather than regex
   // private static final Pattern WIKI_LINK_PATTERN =
   // Pattern.compile("\\[\\[\\s*(\\:\\s*)?\\s*(.+?)(\\s*\\|\\s*(.+))?\\s*\\]\\]([a-z]*)");
   raw = raw.substring(raw.indexOf("[[") + 2, raw.lastIndexOf("]]")).trim();
   // parse in case there is a template or magic word - [[{{PAGENAME}}]]
   raw = JFlexParserUtil.parseFragment(parserInput, parserOutput, raw, JFlexParser.MODE_TEMPLATE);
   boolean colon = false;
   if (raw.startsWith(":")) {
     colon = true;
     raw = raw.substring(1).trim();
   }
   String text = null;
   int pos = raw.indexOf('|');
   if (pos != -1 && pos != (raw.length() - 1)) {
     text = raw.substring(pos + 1).trim();
     raw = raw.substring(0, pos).trim();
   }
   String virtualWiki = parserInput.getVirtualWiki();
   WikiLink wikiLink = LinkUtil.parseWikiLink(virtualWiki, raw);
   if (!colon && wikiLink.getNamespace().getId().equals(Namespace.CATEGORY_ID)) {
     // do not set default text for categories
     wikiLink.setText(null);
   }
   if (wikiLink.getVirtualWiki() != null
       && !StringUtils.equals(wikiLink.getVirtualWiki().getName(), virtualWiki)
       && StringUtils.isBlank(wikiLink.getDestination())) {
     // use the root topic name as the destination
     wikiLink.setDestination(wikiLink.getVirtualWiki().getRootTopicName());
     if (StringUtils.isBlank(wikiLink.getText())) {
       wikiLink.setText(wikiLink.getVirtualWiki().getName() + Namespace.SEPARATOR);
     }
   }
   if (wikiLink.getInterwiki() != null
       && StringUtils.isBlank(wikiLink.getDestination())
       && StringUtils.isBlank(wikiLink.getText())) {
     wikiLink.setText(wikiLink.getInterwiki().getInterwikiPrefix() + Namespace.SEPARATOR);
   }
   wikiLink.setColon(colon);
   if (text != null) {
     wikiLink.setText(text);
   }
   if (!StringUtils.isBlank(suffix)) {
     wikiLink.setText(wikiLink.getText() + suffix);
   }
   return wikiLink;
 }
Exemple #4
0
 /**
  * Allowing Javascript action tags to be used as attributes (onmouseover, etc) is a bad thing, so
  * clean up HTML tags to remove any such attributes.
  */
 protected static String validateHtmlTag(String tag) {
   String[] tagInfo = JFlexParserUtil.parseHtmlTag(tag);
   String tagOpen = tagInfo[2];
   String tagKeyword = tagInfo[0];
   String attributes = tagInfo[1];
   String tagClose = tagInfo[3];
   String result = "<";
   if (tagOpen.indexOf('/') != -1) {
     result += "/";
   }
   result += tagKeyword;
   if (!StringUtils.isBlank(attributes)) {
     result += " " + attributes;
   }
   if (tagClose.indexOf('/') != -1) {
     tagClose = " />";
   }
   result += tagClose.trim();
   return result;
 }
Exemple #5
0
 /**
  * Given an HTML tag, split it into its tag type and tag attributes, cleaning up the attribtues in
  * the process - allowing Javascript action tags to be used as attributes (onmouseover, etc) is a
  * bad thing, so clean up HTML tags to remove any such attributes.
  */
 protected static String[] parseHtmlTag(String tag) {
   Matcher m = TAG_PATTERN.matcher(tag);
   String[] result = new String[4];
   if (!m.find()) {
     logger.severe("Failure while attempting to match html tag for pattern " + tag);
     return result;
   }
   String tagType = m.group(2).toLowerCase().trim();
   String tagAttributes = m.group(3).trim();
   String tagOpen = m.group(1).trim();
   String tagClose = m.group(5).trim();
   if (!StringUtils.isBlank(tagAttributes)) {
     tagAttributes = JFlexParserUtil.validateHtmlTagAttributes(tagAttributes).trim();
   }
   result[0] = tagType;
   result[1] = tagAttributes;
   result[2] = tagOpen;
   result[3] = tagClose;
   return result;
 }
Exemple #6
0
 private String processLinkMetadata(
     ParserInput parserInput, ParserOutput parserOutput, int mode, String raw, WikiLink wikiLink)
     throws ParserException {
   String result = raw;
   if (!wikiLink.getColon() && wikiLink.getNamespace().getId().equals(Namespace.CATEGORY_ID)) {
     String sortKey = wikiLink.getText();
     if (!StringUtils.isBlank(sortKey)) {
       sortKey = JFlexParserUtil.parseFragment(parserInput, sortKey, JFlexParser.MODE_PREPROCESS);
     }
     parserOutput.addCategory(wikiLink.getDestination(), sortKey);
     if (mode > JFlexParser.MODE_MINIMAL) {
       // keep the category around in minimal parsing mode, otherwise suppress it from the output
       result = "";
     }
   }
   if (!StringUtils.isBlank(wikiLink.getDestination())) {
     parserOutput.addLink(wikiLink.getDestination());
   }
   return result;
 }
 /** Process all text inside of the equals signs. */
 private String processTocText(
     ParserInput parserInput, ParserOutput parserOutput, String tagText, int mode)
     throws ParserException {
   return JFlexParserUtil.parseFragmentNonLineStart(parserInput, parserOutput, tagText, mode);
 }
Exemple #8
0
 /** Parse a Mediawiki link of the form "[[topic|text]]" and return the resulting HTML output. */
 public String parse(JFlexLexer lexer, String raw, Object... args) throws ParserException {
   boolean containsNestedLinks =
       (args.length > 0 && StringUtils.equals(args[0].toString(), "nested"));
   WikiLink wikiLink = JFlexParserUtil.parseWikiLink(lexer.getParserInput(), raw);
   if (StringUtils.isBlank(wikiLink.getDestination())
       && StringUtils.isBlank(wikiLink.getSection())) {
     // no destination or section
     return raw;
   }
   if (containsNestedLinks) {
     // if there is a nested link it must be an image, otherwise the syntax is invalid.
     if (wikiLink.getColon() || !wikiLink.getNamespace().getId().equals(Namespace.FILE_ID)) {
       int start = raw.indexOf("[[");
       int end = raw.lastIndexOf("]]");
       String content = raw.substring(start + "[[".length(), end);
       return "[["
           + JFlexParserUtil.parseFragment(lexer.getParserInput(), content, lexer.getMode())
           + "]]";
     }
   }
   raw =
       this.processLinkMetadata(
           lexer.getParserInput(), lexer.getParserOutput(), lexer.getMode(), raw, wikiLink);
   if (lexer.getMode() <= JFlexParser.MODE_PREPROCESS) {
     // do not parse to HTML when in preprocess mode
     return raw;
   }
   if (!wikiLink.getColon() && wikiLink.getNamespace().getId().equals(Namespace.FILE_ID)) {
     // parse as an image
     return lexer.parse(JFlexLexer.TAG_TYPE_IMAGE_LINK, raw);
   }
   try {
     if (!StringUtils.isBlank(wikiLink.getInterWiki())) {
       // inter-wiki link
       return LinkUtil.interWiki(wikiLink);
     }
     String virtualWiki = lexer.getParserInput().getVirtualWiki();
     if (wikiLink.getVirtualWiki() != null) {
       // link to another virtual wiki
       virtualWiki = wikiLink.getVirtualWiki().getName();
     }
     if (StringUtils.isBlank(wikiLink.getText())
         && !StringUtils.isBlank(wikiLink.getDestination())) {
       wikiLink.setText(wikiLink.getDestination());
       if (!StringUtils.isBlank(wikiLink.getSection())) {
         wikiLink.setText(
             wikiLink.getText()
                 + "#"
                 + Utilities.decodeAndEscapeTopicName(wikiLink.getSection(), true));
       }
     } else if (StringUtils.isBlank(wikiLink.getText())
         && !StringUtils.isBlank(wikiLink.getSection())) {
       wikiLink.setText(Utilities.decodeAndEscapeTopicName("#" + wikiLink.getSection(), true));
     } else {
       // pass a parameter via the parserInput to prevent nested links from being generated
       lexer.getParserInput().getTempParams().put(LINK_CAPTION, true);
       wikiLink.setText(
           JFlexParserUtil.parseFragment(
               lexer.getParserInput(), wikiLink.getText(), lexer.getMode()));
       lexer.getParserInput().getTempParams().remove(LINK_CAPTION);
     }
     if (StringUtils.equals(wikiLink.getDestination(), lexer.getParserInput().getTopicName())
         && StringUtils.equals(virtualWiki, lexer.getParserInput().getVirtualWiki())
         && StringUtils.isBlank(wikiLink.getSection())) {
       // same page, bold the text and return
       return "<b>"
           + (StringUtils.isBlank(wikiLink.getText())
               ? wikiLink.getDestination()
               : wikiLink.getText())
           + "</b>";
     }
     // do not escape text html - already done by parser
     return LinkUtil.buildInternalLinkHtml(
         lexer.getParserInput().getContext(),
         virtualWiki,
         wikiLink,
         wikiLink.getText(),
         null,
         null,
         false);
   } catch (DataAccessException e) {
     logger.severe("Failure while parsing link " + raw, e);
     return "";
   } catch (ParserException e) {
     logger.severe("Failure while parsing link " + raw, e);
     return "";
   }
 }