// Path: app/src/main/java/org/cnodejs/android/md/model/api/ApiDefine.java
// public final class ApiDefine {
// 
//     private ApiDefine() {}
// 
//     public static final String HOST_BASE_URL = "https://cnodejs.org";
//     public static final String API_BASE_URL = HOST_BASE_URL + "/api/v1/";
//     public static final String USER_PATH_PREFIX = "/user/";
//     public static final String USER_LINK_URL_PREFIX = HOST_BASE_URL + USER_PATH_PREFIX;
//     public static final String TOPIC_PATH_PREFIX = "/topic/";
//     public static final String TOPIC_LINK_URL_PREFIX = HOST_BASE_URL + TOPIC_PATH_PREFIX;
// 
//     public static final String USER_AGENT = "CNodeMD/" + BuildConfig.VERSION_NAME + " (Android " + Build.VERSION.RELEASE + "; " + Build.MODEL + "; " + Build.MANUFACTURER + ")";
// 
//     public static final boolean MD_RENDER = true; // 使用服务端Markdown渲染可以轻微提升性能
// 
// }


import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import org.cnodejs.android.md.model.api.ApiDefine;
import org.joda.time.DateTime;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.safety.Cleaner;
import org.jsoup.safety.Whitelist;
import org.tautua.markdownpapers.Markdown;
import org.tautua.markdownpapers.parser.ParseException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.UUID;
    }

    /**
     * CNode兼容性的Markdown转换
     * '@'协议转换为'/user/'相对路径
     * 解析裸链接
     * 最外层包裹'<div class="markdown-text"></div>'
     * 尽可能的实现和服务端渲染相同的结果
     */

    private static final Markdown md = new Markdown();

    public static String renderMarkdown(String text) {
        // 保证text不为null
        text = TextUtils.isEmpty(text) ? "" : text;
        // 解析@协议
        text = text.replaceAll("@([\\w\\-]+)\\b(?![]<.])", "[@$1](" + ApiDefine.USER_PATH_PREFIX + "$1)");
        // 解析裸链接
        text = text + " ";
        text = text.replaceAll("((http|https|ftp)://[\\w\\-.:/?=&#%]+)(\\s)", "[$1]($1)$3");
        text = text.trim();
        // 渲染markdown
        try {
            StringWriter out = new StringWriter();
            md.transform(new StringReader(text), out);
            text = out.toString();
        } catch (ParseException e) {
            // nothing to do
        }
        // 添加样式容器
// Path: app/src/main/java/org/cnodejs/android/md/model/api/ApiDefine.java
// public final class ApiDefine {
// 
//     private ApiDefine() {}
// 
//     public static final String HOST_BASE_URL = "https://cnodejs.org";
//     public static final String API_BASE_URL = HOST_BASE_URL + "/api/v1/";
//     public static final String USER_PATH_PREFIX = "/user/";
//     public static final String USER_LINK_URL_PREFIX = HOST_BASE_URL + USER_PATH_PREFIX;
//     public static final String TOPIC_PATH_PREFIX = "/topic/";
//     public static final String TOPIC_LINK_URL_PREFIX = HOST_BASE_URL + TOPIC_PATH_PREFIX;
// 
//     public static final String USER_AGENT = "CNodeMD/" + BuildConfig.VERSION_NAME + " (Android " + Build.VERSION.RELEASE + "; " + Build.MODEL + "; " + Build.MANUFACTURER + ")";
// 
//     public static final boolean MD_RENDER = true; // 使用服务端Markdown渲染可以轻微提升性能
// 
// }


// Path: app/src/main/java/org/cnodejs/android/md/util/FormatUtils.java
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import org.cnodejs.android.md.model.api.ApiDefine;
import org.joda.time.DateTime;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.safety.Cleaner;
import org.jsoup.safety.Whitelist;
import org.tautua.markdownpapers.Markdown;
import org.tautua.markdownpapers.parser.ParseException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.UUID;

    }

    /**
     * CNode兼容性的Markdown转换
     * '@'协议转换为'/user/'相对路径
     * 解析裸链接
     * 最外层包裹'<div class="markdown-text"></div>'
     * 尽可能的实现和服务端渲染相同的结果
     */

    private static final Markdown md = new Markdown();

    public static String renderMarkdown(String text) {
        // 保证text不为null
        text = TextUtils.isEmpty(text) ? "" : text;
        // 解析@协议
        text = text.replaceAll("@([\\w\\-]+)\\b(?![]<.])", "[@$1](" + ApiDefine.USER_PATH_PREFIX + "$1)");
        // 解析裸链接
        text = text + " ";
        text = text.replaceAll("((http|https|ftp)://[\\w\\-.:/?=&#%]+)(\\s)", "[$1]($1)$3");
        text = text.trim();
        // 渲染markdown
        try {
            StringWriter out = new StringWriter();
            md.transform(new StringReader(text), out);
            text = out.toString();
        } catch (ParseException e) {
            // nothing to do
        }
        // 添加样式容器
        return "<div class=\"markdown-text\">" + text + "</div>";