2015-08-22 252 views
-2

我有一些代码可以生成一个HTML文件,但是,如果有很多文本,文件会远远高于50MB,有时会达到70-80MB。减少HTML文件大小?

我不希望有一个HTML文件达到​​这个尺寸是诚实的。这是定义HTML模板的源代码。

namespace Structure 
{ 
    const std::string strHTMLTemplate = "<doctype html>" 
             "<html lang=\"en\">" 
             "<head>" 
             "<meta charset=\"utf-8\"/>" 
             "<title>Data Verifier Test Results - {@testdesc}</title>" 
             "<style>" 
             "body {" 
             "\tbackground: none repeat scroll 0 0 #F3F3F4;" 
             "\tcolor: #1E1E1F;" 
             "\tfont-family: \"Segoe UI\",Tahoma,Geneva,Verdana,sans-serif;" 
             "\tmargin: 0;" 
             "\tpadding: 0;" 
             "}" 
             "h1 {" 
             "\tbackground-color: #E2E2E2;" 
             "\tborder-bottom: 1px solid #C1C1C2;" 
             "\tcolor: #201F20;" 
             "\tfont-size: 24pt;" 
             "\tfont-weight: normal;" 
             "\tmargin: 0;" 
             "\tpadding: 10px 0 10px 10px;" 
             "}" 
             "h2 {" 
             "\tfont-size: 21pt;" 
             "\tfont-weight: normal;" 
             "\tmargin: 0;" 
             "\tpadding: 15px 0 5px;" 
             "}" 
             "h3 {" 
             "\tbackground-color: rgba(0, 0, 0, 0);" 
             "\tfont-size: 18pt;" 
             "\tfont-weight: normal;" 
             "\tmargin: 0;" 
             "\tpadding: 15px 0 5px;" 
             "}" 
             "h4 {" 
             "\tbackground-color: rgba(0, 0, 0, 0);" 
             "\tfont-size: 15pt;" 
             "\tfont-weight: normal;" 
             "\tmargin: 0;" 
             "\tpadding: 15px 0 5px;" 
             "}" 
             "h5 {" 
             "\tbackground-color: rgba(0, 0, 0, 0);" 
             "\tfont-size: 12pt;" 
             "\tfont-weight: normal;" 
             "\tmargin: 0;" 
             "\tpadding: 15px 0 5px;" 
             "}" 
             "a {" 
             "\tcolor: #1382CE;" 
             "}" 
             "table {" 
             "\tborder-collapse: collapse;" 
             "\tborder-spacing: 0;" 
             "\tfont-size: 8pt;" 
             "}" 
             "table th {" 
             "\tbackground: none repeat scroll 0 0 #E7E7E8;" 
             "\tfont-weight: normal;" 
             "\tpadding: 1px 2px;" 
             "\ttext-align: left;" 
             "\ttext-decoration: none;" 
             "}" 
             "table td {" 
             "\tbackground: none repeat scroll 0 0 #F7F7F8;" 
             "\tborder: 1px solid #E7E7E8;" 
             "\tmargin: 0;" 
             "\tpadding: 1px 2px 1px 1px;" 
             "\tvertical-align: top;" 
             "}" 
             "" 
             ".textCentered {" 
             "\ttext-align: center;" 
             "}" 
             ".messageCell {" 
             "\twidth: 100;" 
             "}" 
             "#content {" 
             "\tpadding: 0 12px 12px;" 
             "}" 
             "#overview table {" 
             "\tmax-width: 75;" 
             "\twidth: auto;" 
             "}" 
             "#messages table {" 
             "\twidth: 97;" 
             "}" 
             "</style>" 
             "</head>" 
             "<body>" 
             "<div id=\"big_wrapper\">" 
             "\t<h1>Test Results - {@testdesc}</h1>" 
             "\t" 
             "{@eeddata}" 
             "\t" 
             "</body>" 
             "</html>"; 

    const std::string strHTMLEEDDataEntireMsgTemplate = "\t" 
             "\t\t<h2 _locID=\"EEDInfo\">{@indveeddata}</h2>" 
             "{@clientdata}" 
             "\t"; 

    const std::string strHTMLEEDDataTemplate = "\t" 
             "{@clientdata}" 
             "\t"; 

    const std::string strHTMLRequestTemplate = "\t\t<h2 _locID=\"ReqInfo\">Request Information</h2>" 
             "\t\t<div id=\"ReqInfoResults\">" 
             "\t\t</div>" 
             "\t\t<div id=\"ReqValueResults\">" 
             "\t\t<h3 _locID=\"RequestDataTitle\">Request Data</h3>" 
             "\t\t\t<table>" 
             "{@requestbody}" 
             "\t\t\t</table>" 
             "\t\t</div>"; 

    const std::string strHTMLResponseTemplate = "\t\t<h2 _locID=\"ResponseTitle\">Response Results</h2>" 
             "\t\t<div id=\"RespInfoResults\">" 
             "\t\t</div>" 
             "\t\t<div id=\"RespValueResults\">" 
             "\t\t<h3 _locID=\"ResponseDataTitle\">Response Data</h3>" 
             "\t\t\t<table>" 
             "{@responsebody}" 
             "\t\t\t</table>" 
             "\t\t</div>"; 

    const std::string strHTMLSingleMessageTemplate = "\t\t<h4 _locID=\"MessageTitle\">RWF Message</h4>" 
             "\t\t<div id=\"MessageResults\">" 
             "\t\t<h5 _locID=\"ActualMessageResults\">Source: {@source} Destination: {@destination} Timestamp: {@timestamp}</h5>" 
             "{@messagebody}" 
             "\t\t</div>"; 

    const std::string strHTMLClientDataTemplate = "\t\t<h3 _locID=\"Client ID: \">{@clientid} {@subclientid}</h3>" 
               "{@requestmsg}" 
               "{@responsemsg}"; 

    const std::string strHTMLResponseHdrTemplate = "<tr>" 
                "<th></th>" 
                "<th _locID=\"TypeHeader\">Type</th>" 
                "<th _locID=\"ValueHeader\">Value</th>" 
                "</tr>"  
                "<tr>" 
                "<td style=\"background: blue\"></td>" 
                "<td><strong>Path</strong></td>" 
                "<td>{@url}</td>" 
                "</tr>" 
                "<tr>" 
                "<td style=\"background: blue\"></td>" 
                "<td><strong>IP Address</strong></td>" 
                "<td>{@iptosendto}</td>" 
                "</tr>"; 

    const std::string strHTMLReqHdrRowTemplate = "<tr>" 
               "<th></th>" 
               "{@reqhdrrowvalue}" 
               "</tr>"; 

    const std::string strHTMLRespHdrRowTemplate = "<tr>" 
               "<th></th>" 
               "{@resphdrrowvalue}" 
               "</tr>"; 

    const std::string strHTMLReqHdrRowValueTemplate = "<th _locID=\"{@requestvaluehdr}Header\"><strong>{@requestvaluehdr}<strong></th>"; 

    const std::string strHTMLRespHdrRowValueTemplate = "<th _locID=\"{@responsevaluehdr}Header\"><strong>{@responsevaluehdr}<strong></th>"; 

    const std::string strHTMLReqDataRowTemplate = "<tr>" 
               "<td style=\"background: blue\"></td>" 
               "{@reqdatarowvalue}" 
               "</tr>"; 

    const std::string strHTMLRespDataRowTemplate = "<tr>" 
               "<td style=\"background: blue\"></td>" 
               "{@respdatarowvalue}" 
               "</tr>"; 

    const std::string strHTMLRespDataRowValueTemplate = "<td>{@responsevalue}</td>"; 
    const std::string strHTMLReqDataRowValueTemplate = "<td>{@requestvalue}</td>"; 

    const std::string strHTMLCLIENTDATA = "{@clientdata}"; 
    const std::string strHTMLEEDSDATA = "{@eeddata}"; 
    const std::string strHTMLINDVEEDDATA = "{@indveeddata}"; 

    const std::string strHTMLCLIENTID = "{@clientid}"; 
    const std::string strHTMLSUBCLIENTID = "{@subclientid}"; 

    const std::string strHTMLSOURCEIP = "{@source}"; 
    const std::string strHTMLDESTINATIONIP = "{@destination}"; 
    const std::string strHTMLTIMESTAMP = "{@timestamp}"; 

    const std::string strHTMLREQUESTMSG = "{@requestmsg}"; 
    const std::string strHTMLRESPONSEMSG = "{@responsemsg}"; 

    const std::string strHTMLREQUESTBODY = "{@requestbody}"; 
    const std::string strHTMLRESPONSEBODY = "{@responsebody}"; 
    const std::string strHTMLMESSAGEBODY = "{@messagebody}"; 

    const std::string strHTMLREQHDRROWVALUE = "{@reqhdrrowvalue}"; 
    const std::string strHTMLRESPHDRROWVALUE = "{@resphdrrowvalue}"; 

    const std::string strHTMLREQUESTHDR = "{@requesthdr}"; 
    const std::string strHTMLRESPONSEHDR = "{@responsehdr}"; 

    const std::string strHTMLREQUESTVALUEHDR = "{@requestvaluehdr}"; 
    const std::string strHTMLRESPONSEVALUEHDR = "{@responsevaluehdr}"; 

    const std::string strHTMLREQDATAROWVALUE = "{@reqdatarowvalue}"; 
    const std::string strHTMLRESPDATAROWVALUE = "{@respdatarowvalue}"; 

    const std::string strHTMLREQUESTVALUE = "{@requestvalue}"; 
    const std::string strHTMLRESPONSEVALUE = "{@responsevalue}"; 
    const std::string strHTMLTESTDESCRIPTION = "{@testdesc}"; 
} 

有超过1000调用,使用strHTMLReqDataRowTemplate和strHTMLRespDataRowTemplate并提供必要的数据替换{@}标签。这一切都是递归完成的。

看看HTML和CSS,你们能看到我能改善输出的任何方式吗?

HTML页面应该有行和列的数据,而且我经常创建一个包含行和列的表。

+0

你不能“调用”一个变量。你所看到的不是实际的代码。无论如何,一块实际的HTML文件会更好。 (但是不希望有太大的改进,HTML不是一个可以尽可能降低质量的图像,它是文本。) – deviantfan

+0

重写它。谢谢。 –

+0

看看文本内容,在我看来''HTML/CSS'只是替代'> 50mb'后的引用输出大小的一小部分。所以我不认为减少HTML/CSS会给你带来什么是诚实的。与*速度*更相关的可能是你如何*替代*文本。我会编写一个预处理程序来将文本分割为*替换标记*,并将替换文本与* sliced *文本交错发送。 – Galik

回答

1

我看:

style=\"background: blue\"> 

内联风格一样,消耗了大量的空间。如果可能的话,将其彻底删除,并使用CSS比如:

td:nth-child(2) { ... } 

如果这是不可能的,你可以添加的,而不是一个内嵌样式类,以节省至少几个字节:

<td class="data" 

你甚至可以通过添加自定义属性骗一点:造型为

<td x>Hello</td> 

小例子:

div[x] {color: blue;} 
 
div[y] {color: red;}
<div x>Hello</div> 
 
<div y>world</div>

但是,最终它仍然是大量的数据,所以最好的办法也许是选择一种不同的格式或分配时,压缩HTML文件。即使你从网络服务器提供它,你也可以使用gz来压缩响应,这将大幅度缩小尺寸,而不仅仅是像上面那样的微型优化。

请注意,对于浏览器来说,如果元素包含我的小属性hack的完整类名,则无关紧要。最后,需要将整个文档加载到内存中,而不管将其存储在磁盘上的紧凑程度如何,具有样式的实际单元格都会占用大量内存。可能有一些差异,但不如您预期的那么多。

1

C++是不要做这样的事情最好的语言,但我不禁想到的功能,这样一个巨大的农场:

std::string strHTMLReqDataRowTemplate (const std::string & reqdatarowvalue) 
{ 
    return "<tr>" 
      "<td style=\"background: blue\"></td>"+ 
      reqdatarowvalue + 
      "</tr>"; 
} 

std::ostream & strHTMLReqDataRowTemplate (std::ostream & out, 
              const std::string & reqdatarowvalue) 
{ 
    out << "<tr>" 
      "<td style=\"background: blue\"></td>"; 
    out << reqdatarowvalue; 
    out << "</tr>"; 
} 

代替字符串场会比搜索和替换标签更快。在一个或多个cpp文件中定义函数,具体取决于库的大小和组织方式,然后你需要公开的是一个包含函数定义的头文件。