2015-08-22 254 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文件中定義函數,具體取決於庫的大小和組織方式,然後你需要公開的是一個包含函數定義的頭文件。