<commit_before>package com.youbenzi.mdtool.export;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import com.youbenzi.mdtool.markdown.Block;
import com.youbenzi.mdtool.markdown.BlockType;
import com.youbenzi.mdtool.markdown.ValuePart;
import com.youbenzi.mdtool.tool.MDTool;
import com.youbenzi.mdtool.tool.Tools;


public class HTMLDecorator implements Decorator{

	private StringBuilder content = new StringBuilder();
	
	public void beginWork(String outputFilePath) {
		
	}
	public void decorate(List<Block> list) {
		for (Block block : list) {
			try{
				String str;
				switch (block.getType()) {
				case CODE:
					str = codeParagraph(block.getValueParts());
					break;
				case HEADLINE:
					str = headerParagraph(block.getValueParts(), block.getLevel());
					break;
				case QUOTE:
					str = quoteParagraph(block.getListData());
					break;
				case TABLE:
					str = tableParagraph(block.getTableData());
					break;
				case LIST:
					str = listParagraph(block.getListData());
					break;
				default:
					str = commonTextParagraph(block.getValueParts());
					break;
				}

				content.append(str);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}

	public void afterWork(String outputFilePath) {
		File file =  new File(outputFilePath);
		FileWriter writer = null;
		try {
			writer = new FileWriter(file);
			writer.write(content.toString());
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				if(writer!=null){
					writer.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public String outputHtml(){
		return content.toString();
	}
	
	private String codeParagraph(ValuePart[] valueParts){

		String value = valueParts[0].getValue();
		StringBuilder tmp = new StringBuilder("<pre>\n");
		tmp.append("<code>");
		value = value.replaceAll("<", "&lt;");
		value = value.replaceAll(">", "&gt;");
		if(value.endsWith("\n")) {
			value = value.substring(0, value.length() - "\n".length());
		}
		tmp.append(value);
		tmp.append("</code>\n");
		tmp.append("</pre>\n");
		
		return tmp.toString();
	}
	
	private String headerParagraph(ValuePart[] valueParts, int level){
		level = level + 1;
		StringBuilder tmp = new StringBuilder("<h"+level+">");
		
		for (ValuePart valuePart : valueParts) {
			BlockType[] types = valuePart.getTypes();
			String value  = valuePart.getValue();
			if(types!=null){<commit_msg>修改代码的html格式<commit_after>package com.youbenzi.mdtool.export;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import com.youbenzi.mdtool.markdown.Block;
import com.youbenzi.mdtool.markdown.BlockType;
import com.youbenzi.mdtool.markdown.ValuePart;
import com.youbenzi.mdtool.tool.MDTool;
import com.youbenzi.mdtool.tool.Tools;


public class HTMLDecorator implements Decorator{

	private StringBuilder content = new StringBuilder();
	
	public void beginWork(String outputFilePath) {
		
	}
	public void decorate(List<Block> list) {
		for (Block block : list) {
			try{
				String str;
				switch (block.getType()) {
				case CODE:
					str = codeParagraph(block.getValueParts());
					break;
				case HEADLINE:
					str = headerParagraph(block.getValueParts(), block.getLevel());
					break;
				case QUOTE:
					str = quoteParagraph(block.getListData());
					break;
				case TABLE:
					str = tableParagraph(block.getTableData());
					break;
				case LIST:
					str = listParagraph(block.getListData());
					break;
				default:
					str = commonTextParagraph(block.getValueParts());
					break;
				}

				content.append(str);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}

	public void afterWork(String outputFilePath) {
		File file =  new File(outputFilePath);
		FileWriter writer = null;
		try {
			writer = new FileWriter(file);
			writer.write(content.toString());
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				if(writer!=null){
					writer.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public String outputHtml(){
		return content.toString();
	}
	
	private String codeParagraph(ValuePart[] valueParts){

		String value = valueParts[0].getValue();
		StringBuilder tmp = new StringBuilder("<pre>");
		tmp.append("<code>");
		value = value.replaceAll("<", "&lt;");
		value = value.replaceAll(">", "&gt;");
		if(value.endsWith("\n")) {
			value = value.substring(0, value.length() - "\n".length());
		}
		tmp.append(value);
		tmp.append("</code>");
		tmp.append("</pre>\n");
		
		return tmp.toString();
	}
	
	private String headerParagraph(ValuePart[] valueParts, int level){
		level = level + 1;
		StringBuilder tmp = new StringBuilder("<h"+level+">");
		
		for (ValuePart valuePart : valueParts) {
			BlockType[] types = valuePart.getTypes();
			String value  = valuePart.getValue();
			if(types!=null){
{'fraction_non_alphanumeric': 0.1453654552878726, 'fraction_numerical': 0.0012249897917517355, 'mean_word_length': 3.643601895734597, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, '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': '3953915', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1542, 'n_words': 331}