BBCodeMuncher.java :  » App » facepunch4droid » com » uk » lex » facepunch » Android Open Source

Android Open Source » App » facepunch4droid 
facepunch4droid » com » uk » lex » facepunch » BBCodeMuncher.java
package com.uk.lex.facepunch;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BBCodeMuncher {
  private static final int ci = Pattern.CASE_INSENSITIVE;
  private static final Pattern bground = Pattern.compile("(\\[background.*?\\])");
  private static final Pattern h2tab = Pattern.compile("\\[(/)?(h2)?(tab)?\\]", ci);
  private static final Pattern highlights = Pattern.compile("\\[highlight\\]", ci);
  private static final Pattern highlighte = Pattern.compile("\\[/highlight\\]", ci);
  private static final Pattern editline = Pattern.compile("\\[editline\\].*?\\[/editline\\]", ci);
  private static final Pattern codeblockx = Pattern.compile("\\[(/)?(html)?(php)?(lua)?\\]", ci);
  private static final Pattern codeblocks = Pattern.compile("\\[code\\]", ci);
  private static final Pattern codeblocke = Pattern.compile("\\[/code\\]", ci);
  private static final Pattern releases = Pattern.compile("\\[release\\]", ci);
  private static final Pattern releasee = Pattern.compile("\\[/release\\]", ci);
  private static final Pattern quotea = Pattern.compile("\\[quote=?\"?(.*?);(\\d*?)\"?\\]", ci);
  private static final Pattern quoteb = Pattern.compile("\\[quote=?\"?(.*?)\"?\\]", ci);
  private static final Pattern quotec = Pattern.compile("\\[quote\\]", ci);
  private static final Pattern quotee = Pattern.compile("\\[/quote\\]", ci);
  private static final String bgroundr = "[noparse]$1[/noparse]";
  private static final String h2tabr = "[$1b]";
  private static final String highlightsr = "[b][color=red]";
  private static final String highlighter = "[/color][/b]";
  private static final String editliner = "[b]Edited:[/b]";
  private static final String codeblockxr = "[$1code]";
  private static final String codeblocksr = "[background=gray]";
  private static final String codeblocker = "[/background]";
  private static final String releasesr = "[background=darkgray]";
  private static final String releaseer = "[/background]";
  private static final String quotear = "[background=blue][b]$1 posted:[/b]\n";
  private static final String quotebr = "[background=blue][b]$1 posted:[/b]\n";
  private static final String quotecr = "[background=blue]";
  private static final String quoteer = "[/background]"; 
  
  /**
   * Condense abstract BBCodeses
   * @param post The FP BBCode
   * @return A post with rather less FP BBCode
   */
  public static String munchBBCode(String post) {
    String ret;
    ret = rpl(mtchr(bground,    post), bgroundr);
    ret = rpl(mtchr(h2tab,      ret), h2tabr);
    ret = rpl(mtchr(highlights, ret), highlightsr);
    ret = rpl(mtchr(highlighte, ret), highlighter);
    ret = rpl(mtchr(editline,   ret), editliner);
    ret = rpl(mtchr(codeblockx, ret), codeblockxr);
    ret = rpl(mtchr(codeblocks, ret), codeblocksr);
    ret = rpl(mtchr(codeblocke, ret), codeblocker);
    ret = rpl(mtchr(releases,   ret), releasesr);
    ret = rpl(mtchr(releasee,   ret), releaseer);
    ret = rpl(mtchr(quotea,     ret), quotear);
    ret = rpl(mtchr(quoteb,     ret), quotebr);
    ret = rpl(mtchr(quotec,     ret), quotecr);
    ret = rpl(mtchr(quotee,     ret), quoteer);
    return ret;
  }
  private static String rpl(Matcher a, String c) {
    return a.replaceAll(c);
  }
  private static Matcher mtchr(Pattern b, String c) {
    return b.matcher(c);
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.