Ejemplo n.º 1
0
  @Override
  public void appendInternalLink(
      String topic,
      String hashSection,
      String topicDescription,
      String cssClass,
      boolean parseRecursive) {
    try {
      String virtualWiki = fParserInput.getVirtualWiki();
      WikiLink wikiLink;
      if (hashSection != null) {
        wikiLink = LinkUtil.parseWikiLink(virtualWiki, topic + "#" + hashSection);
      } else {
        wikiLink = LinkUtil.parseWikiLink(virtualWiki, topic);
      }
      String destination = wikiLink.getDestination();
      String section = wikiLink.getSection();
      String query = wikiLink.getQuery();
      String href = buildTopicUrlNoEdit(fContextPath, virtualWiki, destination, section, query);
      String style = "";
      if (StringUtils.isBlank(topic) && !StringUtils.isBlank(section)) {
        // do not check existence for section links
      } else {
        String articleName = topic.replace('_', ' ');
        if (LinkUtil.isExistingArticle(virtualWiki, articleName) == null) {
          style = "edit";
          href = LinkUtil.buildEditLinkUrl(fContextPath, virtualWiki, topic, query, -1);
        }
      }
      WPATag aTagNode = new WPATag();
      aTagNode.addAttribute("href", href, true);
      aTagNode.addAttribute("class", style, true);
      aTagNode.addObjectAttribute("wikilink", topic);

      pushNode(aTagNode);
      if (parseRecursive) {
        WikipediaParser.parseRecursive(topicDescription.trim(), this, false, true);
      } else {
        aTagNode.addChild(new ContentToken(topicDescription));
      }
      popNode();
    } catch (DataAccessException e1) {
      e1.printStackTrace();
      append(new ContentToken(topicDescription));
    }
  }
Ejemplo n.º 2
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 "";
   }
 }