static String getHtml(Article article) {
        String csspath = article.getTemplate().getSavePath();
        String content = article.getArticleContent().getContent();
        StringBuffer headStr = new StringBuffer();
        String style = article.getArticleContent().getStyle();
        String eg_pic = "";
        String pic_size = "@1000w_1o.jpg";

        Document doc = Jsoup.parse(content.replaceAll(" replaced=\"true\"", ""));
        Elements body = doc.body().children();

        //内容图片此尺寸控制
        String pic_class = "figure > img";
        Elements es_pic = doc.select(pic_class);
        int pic_index = 0;
        for (Element picdesc : es_pic) {
            String rp_src = picdesc.attr("src") + pic_size;
            body.select("figure > img").get(pic_index).attr("src", rp_src);
            eg_pic = pic_index == 0 ? rp_src : eg_pic;
            pic_index++;
        }

        //头图片尺寸
        String pic_header_class = "header > img";
        Elements es_header_pic = doc.select(pic_header_class);
        for (Element pic_header : es_header_pic) {
            String old_pic_header = pic_header.attr("src");
            String rp_h_src = old_pic_header + pic_size;
            body.select("header > img").get(0).attr("src", rp_h_src);
        }

        Document body_new = Jsoup.parse(body.toString());
        //OGP
        String img = article.getCoverUrl();
        if (null == img || img.isEmpty()) {
            if (eg_pic.isEmpty()) {
                img = "https://www.duweixin.net/app/img/logo.jpg";
            } else {
                img = eg_pic.replace("@1000w_1o.jpg", "@400w_1o.jpg");
            }
        } else {
            img += "@400w_1o.jpg";
        }

        StringBuffer styleBuffer = new StringBuffer();
        //字体样式
        if (null != style && !style.isEmpty()) {
            styleBuffer.append("<style>\n");
            String[] styles = style.split(",");
            for (int i = 0; i < styles.length; i++) {
                String st = styles[i];
                Long uptime = article.getUpdateTime().getTime();
                String nyr = DateUtils.toStrFromLong(article.getCreateTime().getTime(), "yyyyMMdd");
                String id = article.getId();
                styleBuffer.append("@font-face{font-family:\"" + st + "\";src:url(\"https://font.zi.com/" + nyr + "/" + id + "_" + st + ".bmp?v=" + uptime + "\") format(\"truetype\");}"+ st +"{font-family:\"" + st + "”;}");
            }
            styleBuffer.append("\n</style>");
        }

        headStr.append("<meta property=\"og:title\" content=\"" + article.getTitle() + "\" />");
        headStr.append("<meta property=\"og:url\" content=\"https://zi.com/w/a?id=" + article.getId() + "\" />");
        headStr.append("<meta property=\"og:description\" content=\"" + article.getDesc().replaceAll("\n", "") + "\" />");
        headStr.append("<meta property=\"og:site_name\" content=\"字里行间\" />");
        headStr.append("<meta property=\"og:image\" content=\"" + img + "\" />");
        headStr.append("<meta property=\"og:image:type\" content=\"image/jpeg\" />");
        headStr.append("<meta property=\"og:image:width\" content=\"400\" />");

        headStr.append("<script type=\"text/javascript\" src=\"js/zepto.min.js\"></script>\n");
        headStr.append("<script type=\"text/javascript\" src=\"js/g.js\"></script>\n");
        headStr.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/public.css\"/>\n");
        headStr.append("<link href=\"css/"+csspath+"\" rel=\"stylesheet\" type=\"text/css\" media=\"screen\">");

        headStr.append(styleBuffer.toString());
        Element head_add = doc.select("head").first();
        head_add.append(headStr.toString());
        Element body_add = body_new.select("body").first();
        body_add.append("<script type=\"text/javascript\" src=\"js/article_new.js\"></script>");
        content = "<!doctype html>\n" +
                "<html class=\"html\">\n" + head_add.toString() + "\n" + body_add.toString() +
                "\n</html>";
        return content;
    }

asoren
404 声望15 粉丝

引用和评论

0 条评论