<commit_before>package com.zfdang.zsmth_android.models;

import android.text.Html;

import com.zfdang.zsmth_android.Settings;
import com.zfdang.zsmth_android.helpers.StringUtils;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Post object.
 * Created by zfdang on 2016-3-14.
 */
public class Post {
    private static final String ATTACHMENT_MARK = "###ZSMTH_ATTACHMENT###";
    public static int ACTION_DEFAULT = 0;
    public static int ACTION_FIRST_POST_IN_SUBJECT = 1;
    public static int ACTION_PREVIOUS_POST_IN_SUBJECT = 2;
    public static int ACTION_NEXT_POST_IN_SUBJECT = 3;
    private String postID;
    private String title;
    private String author;
    private String nickName;
    private Date date;
    private String position;

    private List<String> likes;
    private List<Attachment> attachFiles;

    private String htmlContent; // likes are not included
    //    private String htmlCompleteContent; // likes are included
    private List<ContentSegment> mSegments;  // parsed from htmlCompleteContent

    public Post() {
        date = new Date();
    }

    public String getPostID() {
        return postID;
    }

    public void setPostID(String postID) {
        this.postID = postID;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        if (nickName == null || nickName.length() == 0) {
            return this.author;
        } else {
            return String.format("%s(%s)", this.author, this.nickName);
        }
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getRawAuthor() {
        return this.author;
    }

    public void setNickName(String nickName) {
        final int MAX_NICKNAME_LENGTH = 12;
        if (nickName.length() > MAX_NICKNAME_LENGTH) {
            nickName = nickName.substring(0, MAX_NICKNAME_LENGTH) + "..";
        }
        this.nickName = nickName;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getFormatedDate() {
        return StringUtils.getFormattedString(this.date);
    }


    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }


    // set likes and content together, then merge them to htmlCompleteContent, then split it into htmlSegments
    public void setLikesAndPostContent(List<String> likes, Element content) {
        // save likes
        this.likes = likes;

//                Log.d("setLikesAndPostContent", content.html());

        // find all attachment from node
        // <a target="_blank" href="http://att.newsmth.net/nForum/att/AutoWorld/1939790539/4070982">
        // <img border="0" title="单击此查看原图" src="http://att.newsmth.net/nForum/att/AutoWorld/1939790539/4070982/large" class="resizeable">
        // </a>
        Elements as = content.select("a[href]");
        for (Element a : as) {
            Elements imgs = a.select("img[src]");
            if (imgs.size() == 1) {
                // find one image attachment
                String origImageSrc = a.attr("href");
                if (origImageSrc != null && origImageSrc.startsWith("/nForum")) {
                    origImageSrc = "http://att.newsmth.net" + origImageSrc;
                }

                Element img = imgs.first();
                String resizedImageSrc = img.attr("src");
                if (resizedImageSrc != null && resizedImageSrc.startsWith("/nForum")) {
                    resizedImageSrc = "http://att.newsmth.net" + resizedImageSrc;
                }

                Attachment attach = new Attachment(origImageSrc, resizedImageSrc);
                this.addAttachFile(attach);

                // replace a[href] with MARK
                // we will split the string with MARK, so make sure no two MAKR will stay together
                a.html(ATTACHMENT_MARK + " ");
            }
        }

        // process pure post content
        String formattedPlainText = Html.fromHtml(content.html()).toString();
        this.htmlContent = this.processPostContent(formattedPlainText);

        // it's important to know that not all HTML tags are supported by Html.fromHtml, see the supported list
        // https://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html
        // http://stackoverflow.com/questions/18295881/android-textview-html-font-size-tag
        String htmlCompleteContent = this.htmlContent;

        if (likes != null && likes.size() > 0) {
            StringBuilder wordList = new StringBuilder();
            wordList.append("<br/><cite>");
            for (String word : likes) {
                wordList.append(word).append("<br/>");
            }
            wordList.append("</cite>");
            htmlCompleteContent += new String(wordList);
        }
        // now htmlCompleteContent has both post content and likes content

        // parse htmlCompleteContent to htmlSegments
        parseContentToSegments(htmlCompleteContent);
    }

    // split complete content with ATTACHMENT_MARK
    private void parseContentToSegments(String htmlCompleteContent) {
        if (mSegments == null) {
            mSegments = new ArrayList<>();
        }
        mSegments.clear();

        if (attachFiles == null || attachFiles.size() == 0) {
            // no attachment, add all content as one segment<commit_msg>change font size of likes<commit_after>package com.zfdang.zsmth_android.models;

import android.text.Html;

import com.zfdang.zsmth_android.Settings;
import com.zfdang.zsmth_android.helpers.StringUtils;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Post object.
 * Created by zfdang on 2016-3-14.
 */
public class Post {
    private static final String ATTACHMENT_MARK = "###ZSMTH_ATTACHMENT###";
    public static int ACTION_DEFAULT = 0;
    public static int ACTION_FIRST_POST_IN_SUBJECT = 1;
    public static int ACTION_PREVIOUS_POST_IN_SUBJECT = 2;
    public static int ACTION_NEXT_POST_IN_SUBJECT = 3;
    private String postID;
    private String title;
    private String author;
    private String nickName;
    private Date date;
    private String position;

    private List<String> likes;
    private List<Attachment> attachFiles;

    private String htmlContent; // likes are not included
    //    private String htmlCompleteContent; // likes are included
    private List<ContentSegment> mSegments;  // parsed from htmlCompleteContent

    public Post() {
        date = new Date();
    }

    public String getPostID() {
        return postID;
    }

    public void setPostID(String postID) {
        this.postID = postID;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        if (nickName == null || nickName.length() == 0) {
            return this.author;
        } else {
            return String.format("%s(%s)", this.author, this.nickName);
        }
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getRawAuthor() {
        return this.author;
    }

    public void setNickName(String nickName) {
        final int MAX_NICKNAME_LENGTH = 12;
        if (nickName.length() > MAX_NICKNAME_LENGTH) {
            nickName = nickName.substring(0, MAX_NICKNAME_LENGTH) + "..";
        }
        this.nickName = nickName;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getFormatedDate() {
        return StringUtils.getFormattedString(this.date);
    }


    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }


    // set likes and content together, then merge them to htmlCompleteContent, then split it into htmlSegments
    public void setLikesAndPostContent(List<String> likes, Element content) {
        // save likes
        this.likes = likes;

//                Log.d("setLikesAndPostContent", content.html());

        // find all attachment from node
        // <a target="_blank" href="http://att.newsmth.net/nForum/att/AutoWorld/1939790539/4070982">
        // <img border="0" title="单击此查看原图" src="http://att.newsmth.net/nForum/att/AutoWorld/1939790539/4070982/large" class="resizeable">
        // </a>
        Elements as = content.select("a[href]");
        for (Element a : as) {
            Elements imgs = a.select("img[src]");
            if (imgs.size() == 1) {
                // find one image attachment
                String origImageSrc = a.attr("href");
                if (origImageSrc != null && origImageSrc.startsWith("/nForum")) {
                    origImageSrc = "http://att.newsmth.net" + origImageSrc;
                }

                Element img = imgs.first();
                String resizedImageSrc = img.attr("src");
                if (resizedImageSrc != null && resizedImageSrc.startsWith("/nForum")) {
                    resizedImageSrc = "http://att.newsmth.net" + resizedImageSrc;
                }

                Attachment attach = new Attachment(origImageSrc, resizedImageSrc);
                this.addAttachFile(attach);

                // replace a[href] with MARK
                // we will split the string with MARK, so make sure no two MAKR will stay together
                a.html(ATTACHMENT_MARK + " ");
            }
        }

        // process pure post content
        String formattedPlainText = Html.fromHtml(content.html()).toString();
        this.htmlContent = this.processPostContent(formattedPlainText);

        // it's important to know that not all HTML tags are supported by Html.fromHtml, see the supported list
        // https://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html
        // http://stackoverflow.com/questions/18295881/android-textview-html-font-size-tag
        String htmlCompleteContent = this.htmlContent;

        if (likes != null && likes.size() > 0) {
            StringBuilder wordList = new StringBuilder();
            wordList.append("<br/><small><cite>");
            for (String word : likes) {
                wordList.append(word).append("<br/>");
            }
            wordList.append("</cite></small>");
            htmlCompleteContent += new String(wordList);
        }
        // now htmlCompleteContent has both post content and likes content

        // parse htmlCompleteContent to htmlSegments
        parseContentToSegments(htmlCompleteContent);
    }

    // split complete content with ATTACHMENT_MARK
    private void parseContentToSegments(String htmlCompleteContent) {
        if (mSegments == null) {
            mSegments = new ArrayList<>();
        }
        mSegments.clear();

        if (attachFiles == null || attachFiles.size() == 0) {
            // no attachment, add all content as one segment
{'fraction_non_alphanumeric': 0.10598755150346278, 'fraction_numerical': 0.012097834662926273, 'mean_word_length': 2.2933025404157044, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 33, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3017267', 'n_tokens_mistral': 3355, 'n_tokens_neox': 3158, 'n_words': 793}