<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace ConvertEquations
{
    /// <summary>
    /// Global and static variables
    /// </summary>
    class Common
    {
        //Filter the src in the html img tag
        public static string HTML_IMG = @"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>";
    }

    /// <summary>
    /// System Code
    /// </summary>
    sealed class ResultCode
    {
        public static string SUCCESS = "转换成功";
        public static string WORD_ERROR = "Word错误";
        public static string WORD_SAVE_ERROR = "Word存储错误";
        public static string EXCEL_ERROR = "Excel错误";
        public static string EXCEL_READ_ERROR = "Excel读取错误";
        public static string FILE_PATH_ERROR = "文件位置错误";
    }
<commit_msg>Add html convert into word<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;

namespace ConvertEquations
{
    /// <summary>
    /// Global and static variables
    /// </summary>
    class Common
    {
        //Filter the src in the html img tag
        public static string HTML_IMG = @"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>";
    }

    /// <summary>
    /// HTML translator util
    /// </summary>
    class HTMLUtils
    {
        // 将HTML代码复制到Windows剪贴板，并保证中
        [DllImport("user32.dll")]
        static extern bool OpenClipboard(IntPtr hWndNewOwner);
        [DllImport("user32.dll")]
        static extern bool EmptyClipboard();
        [DllImport("user32.dll")]
        static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);
        [DllImport("user32.dll")]
        static extern bool CloseClipboard();
        [DllImport("user32.dll", SetLastError = true)]
        static extern uint RegisterClipboardFormatA(string lpszFormat);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr GlobalLock(IntPtr hMem);
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern uint GlobalSize(IntPtr hMem);
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr GlobalUnlock(IntPtr hMem);

        /// <summary>
        /// copy the html into clipboard
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        static public bool CopyHTMLToClipboard(string html)
        {
            uint CF_HTML = RegisterClipboardFormatA("HTML Format");
            bool bResult = false;
            if (OpenClipboard(IntPtr.Zero))
            {
                if (EmptyClipboard())
                {
                    byte[] bs = System.Text.Encoding.UTF8.GetBytes(html);

                    int size = Marshal.SizeOf(typeof(byte)) * bs.Length;

                    IntPtr ptr = Marshal.AllocHGlobal(size);
                    Marshal.Copy(bs, 0, ptr, bs.Length);

                    IntPtr hRes = SetClipboardData(CF_HTML, ptr);
                    CloseClipboard();
                }
            }
            return bResult;
        }

        //将HTML代码按照Windows剪贴板格进行格式化
        public static string HtmlClipboardData(string html)
        {
            StringBuilder sb = new StringBuilder();
            Encoding encoding = Encoding.UTF8; //Encoding.GetEncoding(936);
            string Header = @"Version: 1.0
                            StartHTML: {0:000000}
                            EndHTML: {1:000000}
                            StartFragment: {2:000000}
                            EndFragment: {3:000000}
                            ";
            string HtmlPrefix = @"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">
                            <html>
                            <head>
                            <meta http-equiv=Content-Type content=""text/html; charset={0}"">
                            </head>
                            <body>
                            <!--StartFragment-->
                            ";
            HtmlPrefix = string.Format(HtmlPrefix, "gb2312");

            string HtmlSuffix = @"<!--EndFragment--></body></html>";

            // Get lengths of chunks
            int HeaderLength = encoding.GetByteCount(Header);
            HeaderLength -= 16; // extra formatting characters {0:000000}
            int PrefixLength = encoding.GetByteCount(HtmlPrefix);
            int HtmlLength = encoding.GetByteCount(html);
            int SuffixLength = encoding.GetByteCount(HtmlSuffix);

            // Determine locations of chunks
            int StartHtml = HeaderLength;
            int StartFragment = StartHtml + PrefixLength;
            int EndFragment = StartFragment + HtmlLength;
            int EndHtml = EndFragment + SuffixLength;

            // Build the data
            sb.AppendFormat(Header, StartHtml, EndHtml, StartFragment, EndFragment);
            sb.Append(HtmlPrefix);
            sb.Append(html);
            sb.Append(HtmlSuffix);

            //Console.WriteLine(sb.ToString());
            return sb.ToString();
        }
    }

    /// <summary>
    /// System Code
    /// </summary>
    sealed class ResultCode
    {
        public static string SUCCESS = "转换成功";
        public static string WORD_ERROR = "Word错误";
        public static string WORD_SAVE_ERROR = "Word存储错误";
        public static string EXCEL_ERROR = "Excel错误";
        public static string EXCEL_READ_ERROR = "Excel读取错误";
        public static string FILE_PATH_ERROR = "文件位置错误";
    }

{'fraction_non_alphanumeric': 0.12441760138050043, 'fraction_numerical': 0.011906816220880068, 'mean_word_length': 1.8537666174298375, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, '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': '4615686', 'n_tokens_mistral': 1732, 'n_tokens_neox': 1669, 'n_words': 370}