My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2010 Google Inc. All Rights Reserved.
// Author: bmcquade@google.com (Bryan McQuade)
//
// Simple HTML minifier, based on pagespeed's minify_html.cc

#include <cstdio>
#include <fstream>

#include "base/logging.h"
#include "net/instaweb/htmlparse/public/html_parse.h"
#include "net/instaweb/htmlparse/public/html_writer_filter.h"
#include "net/instaweb/rewriter/public/collapse_whitespace_filter.h"
#include "net/instaweb/rewriter/public/elide_attributes_filter.h"
#include "net/instaweb/rewriter/public/html_attribute_quote_removal.h"
#include "net/instaweb/rewriter/public/remove_comments_filter.h"
#include "net/instaweb/util/public/basictypes.h"
#include "net/instaweb/util/public/file_message_handler.h"
#include "net/instaweb/util/public/string.h"
#include "net/instaweb/util/public/string_util.h"
#include "net/instaweb/util/public/string_writer.h"

namespace {

using net_instaweb::StrCat;

void ReadFileToStringOrDie(const char* filename,
GoogleString* dest) {
std::ifstream file_stream;
file_stream.open(filename, std::ifstream::in | std::ifstream::binary);
CHECK(!file_stream.fail());
dest->assign(std::istreambuf_iterator<char>(file_stream),
std::istreambuf_iterator<char>());
file_stream.close();
CHECK(!file_stream.fail());
}

void WriteStringToFileOrDie(const GoogleString& src,
const char* filename) {
std::ofstream file_stream;
file_stream.open(filename, std::ifstream::out | std::ifstream::binary);
CHECK(!file_stream.fail());
file_stream.write(src.c_str(), src.size());
file_stream.close();
CHECK(!file_stream.fail());
}

class HtmlMinifier {
public:
explicit HtmlMinifier();
~HtmlMinifier();

// Return true if successful, false on error.
bool MinifyHtml(const GoogleString& input_name,
const GoogleString& input,
GoogleString* output);

private:
net_instaweb::FileMessageHandler message_handler_;
net_instaweb::HtmlParse html_parse_;
net_instaweb::RemoveCommentsFilter remove_comments_filter_;
net_instaweb::ElideAttributesFilter elide_attributes_filter_;
net_instaweb::HtmlAttributeQuoteRemoval quote_removal_filter_;
net_instaweb::CollapseWhitespaceFilter collapse_whitespace_filter_;
net_instaweb::HtmlWriterFilter html_writer_filter_;

DISALLOW_COPY_AND_ASSIGN(HtmlMinifier);
};

HtmlMinifier::HtmlMinifier()
: message_handler_(stderr),
html_parse_(&message_handler_),
remove_comments_filter_(&html_parse_),
elide_attributes_filter_(&html_parse_),
quote_removal_filter_(&html_parse_),
collapse_whitespace_filter_(&html_parse_),
html_writer_filter_(&html_parse_) {
html_parse_.AddFilter(&remove_comments_filter_);
html_parse_.AddFilter(&elide_attributes_filter_);
html_parse_.AddFilter(&quote_removal_filter_);
html_parse_.AddFilter(&collapse_whitespace_filter_);
html_parse_.AddFilter(&html_writer_filter_);
}

HtmlMinifier::~HtmlMinifier() {}

bool HtmlMinifier::MinifyHtml(const GoogleString& input_name,
const GoogleString& input,
GoogleString* output) {
net_instaweb::StringWriter string_writer(output);
html_writer_filter_.set_writer(&string_writer);

GoogleString url = StrCat("http://html_minifier.com/", input_name, ".html");
html_parse_.StartParse(url);
html_parse_.ParseText(input.data(), input.size());
html_parse_.FinishParse();

html_writer_filter_.set_writer(NULL);

return true;
}

} // namespace

int main(int argc, char** argv) {
if (argc != 3) {
fprintf(stderr, "Usage: minify_html <input> <output>\n");
return 1;
}

GoogleString original;
ReadFileToStringOrDie(argv[1], &original);

GoogleString minified;
HtmlMinifier html_minifier;
html_minifier.MinifyHtml(argv[1], original, &minified);

WriteStringToFileOrDie(minified, argv[2]);
return 0;
}

Change log

r812 by sligo...@google.com on Jul 7, 2011   Diff
IWYU includes audit... (morlovich)

        It's not safe to access
::CanRewrite from ::Partition as at this
point
        we may already be detached; so do
the checking in Render()
        (which might mean we may end up
throwing out our work in some unfortunate
        circumstances) (morlovich)

        Clean file cache in a background
...
Go to: 
Project members, sign in to write a code review

Older revisions

r666 by abl...@google.com on May 5, 2011   Diff
- libpng-based spriter library
- cleanup serf connections
- massive header/include fixups

r636 by morlov...@google.com on Apr 25, 2011   Diff
- Proper handling of duplicate images
in spriting (abliss)
- Header file cleanups (jmarantz,
jmaessen)
- Cleaner statistics init sequence
...
r611 by jmara...@google.com on Apr 8, 2011   Diff
Add typedef wrapper for std::string.

All revisions of this file

File info

Size: 3912 bytes, 119 lines

File properties

svn:eol-style
LF
Powered by Google Project Hosting