package com.almoturg.sprog.data;

import android.content.Context;

import in.uncod.android.bypass.Bypass;

public class MarkdownConverter {
    private static Bypass bypass;
    private static final Object bypassLock = new Object();
    private Context context;

    public MarkdownConverter(Context context){
        this.context = context;
    }

    public CharSequence convertPoemMarkdown(String markdown, double timestamp) {
        if (timestamp <  1360540800){ // = 2013-02-11
            // Sprog's early poems had paragraph breaks at the end of each line.
            // This regex replaces them with single linebreaks, except between stanzas.
            // (Stanza breaks are detected by checking if the line above is italicized (enclosed in
            // "*"s) while the one below is not, or vice versa. Some lines look like
            // "*lorem ipsum*," (punctuation after the closing "*").
            // There is a special case for one poem with "2." on a single line (c6juhtd).)
            markdown = markdown.replaceAll(
                    "(?:(?<=(?<!\n)\\*[.,]?)\\n\\n(?=\\*))|(?:(?<!(?:\\*[.,]?)|(?:2\\.))\\n\\n(?!\\*))",
                    "  \n");
        }
        return convertMarkdown(markdown);
    }

    public CharSequence convertMarkdown(String markdown) {
        if (bypass == null) {
            synchronized (bypassLock) {
                bypass = new Bypass(context);
            }
        }
        markdown = markdown.replaceAll("(?:^|[^(\\[])(https?://\\S*\\.\\S*)(?:\\s|$)", "[$1]($1)");

        CharSequence converted;
        synchronized (bypassLock) {
            converted = bypass.markdownToSpannable(markdown);
        }
        return converted;
    }
}

@@ -58,16 +58,60 @@
 Context;
+%0Aimport android.text.SpannableStringBuilder;
 %0A%0Aimport
@@ -1463,32 +1463,33 @@
     %7D%0A        %7D%0A
+%0A
         markdown
@@ -1573,16 +1573,441 @@
 $1)%22);%0A%0A
+        // Reddit markdown only interprets underscores as italics if they are not followed by a%0A        // non-whitespace character. E.g. %22_fizz_%22 becomes italic but %22_fizz_buzz%22 does not.%0A        // As the Bypass markdown library does not allow that, we replace such underscores%0A        // with a unicode private use char and substitute back afterwards%0A        markdown = markdown.replaceAll(%22(?=%5C%5CS)_(?=%5C%5CS%7C$)%22,%22%5CuE000%22);%0A%0A
         
@@ -2134,32 +2134,33 @@
 ;%0A        %7D%0A
+%0A
         
 return conve
@@ -2151,24 +2151,414 @@
     
-return converted
+// Can't directly replace in SpannableString so convert to SpannableStringBuilder first.%0A        SpannableStringBuilder converted_builder = new SpannableStringBuilder(converted);%0A        for (int i=0; i%3Cconverted_builder.length();i++) %7B%0A            if (converted_builder.charAt(i) == '%5CuE000') %7B%0A                converted_builder.replace(i,i+1,%22_%22);%0A            %7D%0A        %7D%0A        return converted_builder
 ;%0A  

fix difference between reddit markdown and bypass library markdown