Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(274)

Side by Side Diff: tools/gn/standard_out.cc

Issue 1024783002: Add a --markdown flag to GN to be able to output help in markdown. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@gn_all_help
Patch Set: fix dropped variable Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/gn/docs/reference.md ('k') | tools/gn/switches.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "tools/gn/standard_out.h" 5 #include "tools/gn/standard_out.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 11 matching lines...) Expand all
22 namespace { 22 namespace {
23 23
24 bool initialized = false; 24 bool initialized = false;
25 25
26 #if defined(OS_WIN) 26 #if defined(OS_WIN)
27 HANDLE hstdout; 27 HANDLE hstdout;
28 WORD default_attributes; 28 WORD default_attributes;
29 #endif 29 #endif
30 bool is_console = false; 30 bool is_console = false;
31 31
32 bool is_markdown = false;
33
32 void EnsureInitialized() { 34 void EnsureInitialized() {
33 if (initialized) 35 if (initialized)
34 return; 36 return;
35 initialized = true; 37 initialized = true;
36 38
37 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); 39 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
40 if (cmdline->HasSwitch(switches::kMarkdown)) {
41 // Output help in Markdown's syntax, not color-highlighted.
42 is_markdown = true;
43 }
44
38 if (cmdline->HasSwitch(switches::kNoColor)) { 45 if (cmdline->HasSwitch(switches::kNoColor)) {
39 // Force color off. 46 // Force color off.
40 is_console = false; 47 is_console = false;
41 return; 48 return;
42 } 49 }
43 50
44 #if defined(OS_WIN) 51 #if defined(OS_WIN)
45 // On Windows, we can't force the color on. If the output handle isn't a 52 // On Windows, we can't force the color on. If the output handle isn't a
46 // console, there's nothing we can do about it. 53 // console, there's nothing we can do about it.
47 hstdout = ::GetStdHandle(STD_OUTPUT_HANDLE); 54 hstdout = ::GetStdHandle(STD_OUTPUT_HANDLE);
48 CONSOLE_SCREEN_BUFFER_INFO info; 55 CONSOLE_SCREEN_BUFFER_INFO info;
49 is_console = !!::GetConsoleScreenBufferInfo(hstdout, &info); 56 is_console = !!::GetConsoleScreenBufferInfo(hstdout, &info);
50 default_attributes = info.wAttributes; 57 default_attributes = info.wAttributes;
51 #else 58 #else
52 if (cmdline->HasSwitch(switches::kColor)) 59 if (cmdline->HasSwitch(switches::kColor))
53 is_console = true; 60 is_console = true;
54 else 61 else
55 is_console = isatty(fileno(stdout)); 62 is_console = isatty(fileno(stdout));
56 #endif 63 #endif
57 } 64 }
58 65
59 void WriteToStdOut(const std::string& output) { 66 void WriteToStdOut(const std::string& output) {
60 size_t written_bytes = fwrite(output.data(), 1, output.size(), stdout); 67 size_t written_bytes = fwrite(output.data(), 1, output.size(), stdout);
61 DCHECK_EQ(output.size(), written_bytes); 68 DCHECK_EQ(output.size(), written_bytes);
62 } 69 }
63 70
71 void OutputMarkdownDec(TextDecoration dec) {
72 // The markdown rendering turns "dim" text to italics and any
73 // other colored text to bold.
74
75 #if defined(OS_WIN)
76 DWORD written = 0;
77 if (dec == DECORATION_DIM)
78 ::WriteFile(hstdout, "*", 1, &written, nullptr);
79 else if (dec != DECORATION_NONE)
80 ::WriteFile(hstdout, "**", 2, &written, nullptr);
81 #else
82 if (dec == DECORATION_DIM)
83 WriteToStdOut("*");
84 else if (dec != DECORATION_NONE)
85 WriteToStdOut("**");
86 #endif
87 }
88
64 } // namespace 89 } // namespace
65 90
66 #if defined(OS_WIN) 91 #if defined(OS_WIN)
67 92
68 void OutputString(const std::string& output, TextDecoration dec) { 93 void OutputString(const std::string& output, TextDecoration dec) {
69 EnsureInitialized(); 94 EnsureInitialized();
70 if (is_console) { 95 DWORD written = 0;
96
97 if (is_markdown) {
98 OutputMarkdownDec(dec);
99 } else if (is_console) {
71 switch (dec) { 100 switch (dec) {
72 case DECORATION_NONE: 101 case DECORATION_NONE:
73 break; 102 break;
74 case DECORATION_DIM: 103 case DECORATION_DIM:
75 ::SetConsoleTextAttribute(hstdout, FOREGROUND_INTENSITY); 104 ::SetConsoleTextAttribute(hstdout, FOREGROUND_INTENSITY);
76 break; 105 break;
77 case DECORATION_RED: 106 case DECORATION_RED:
78 ::SetConsoleTextAttribute(hstdout, 107 ::SetConsoleTextAttribute(hstdout,
79 FOREGROUND_RED | FOREGROUND_INTENSITY); 108 FOREGROUND_RED | FOREGROUND_INTENSITY);
80 break; 109 break;
81 case DECORATION_GREEN: 110 case DECORATION_GREEN:
82 // Keep green non-bold. 111 // Keep green non-bold.
83 ::SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN); 112 ::SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN);
84 break; 113 break;
85 case DECORATION_BLUE: 114 case DECORATION_BLUE:
86 ::SetConsoleTextAttribute(hstdout, 115 ::SetConsoleTextAttribute(hstdout,
87 FOREGROUND_BLUE | FOREGROUND_INTENSITY); 116 FOREGROUND_BLUE | FOREGROUND_INTENSITY);
88 break; 117 break;
89 case DECORATION_YELLOW: 118 case DECORATION_YELLOW:
90 ::SetConsoleTextAttribute(hstdout, 119 ::SetConsoleTextAttribute(hstdout,
91 FOREGROUND_RED | FOREGROUND_GREEN); 120 FOREGROUND_RED | FOREGROUND_GREEN);
92 break; 121 break;
93 } 122 }
94 } 123 }
95 124
96 DWORD written = 0;
97 ::WriteFile(hstdout, output.c_str(), static_cast<DWORD>(output.size()), 125 ::WriteFile(hstdout, output.c_str(), static_cast<DWORD>(output.size()),
98 &written, nullptr); 126 &written, nullptr);
99 127
100 if (is_console) 128 if (is_markdown) {
129 OutputMarkdownDec(dec);
130 } else if (is_console) {
101 ::SetConsoleTextAttribute(hstdout, default_attributes); 131 ::SetConsoleTextAttribute(hstdout, default_attributes);
132 }
102 } 133 }
103 134
104 #else 135 #else
105 136
106 void OutputString(const std::string& output, TextDecoration dec) { 137 void OutputString(const std::string& output, TextDecoration dec) {
107 EnsureInitialized(); 138 EnsureInitialized();
108 if (is_console) { 139 if (is_markdown) {
140 OutputMarkdownDec(dec);
141 } else if (is_console) {
109 switch (dec) { 142 switch (dec) {
110 case DECORATION_NONE: 143 case DECORATION_NONE:
111 break; 144 break;
112 case DECORATION_DIM: 145 case DECORATION_DIM:
113 WriteToStdOut("\e[2m"); 146 WriteToStdOut("\e[2m");
114 break; 147 break;
115 case DECORATION_RED: 148 case DECORATION_RED:
116 WriteToStdOut("\e[31m\e[1m"); 149 WriteToStdOut("\e[31m\e[1m");
117 break; 150 break;
118 case DECORATION_GREEN: 151 case DECORATION_GREEN:
119 WriteToStdOut("\e[32m"); 152 WriteToStdOut("\e[32m");
120 break; 153 break;
121 case DECORATION_BLUE: 154 case DECORATION_BLUE:
122 WriteToStdOut("\e[34m\e[1m"); 155 WriteToStdOut("\e[34m\e[1m");
123 break; 156 break;
124 case DECORATION_YELLOW: 157 case DECORATION_YELLOW:
125 WriteToStdOut("\e[33m\e[1m"); 158 WriteToStdOut("\e[33m\e[1m");
126 break; 159 break;
127 } 160 }
128 } 161 }
129 162
130 WriteToStdOut(output.data()); 163 WriteToStdOut(output.data());
131 164
132 if (is_console && dec != DECORATION_NONE) 165 if (is_markdown) {
166 OutputMarkdownDec(dec);
167 } else if (is_console && dec != DECORATION_NONE) {
133 WriteToStdOut("\e[0m"); 168 WriteToStdOut("\e[0m");
169 }
134 } 170 }
135 171
136 #endif 172 #endif
137 173
138 void PrintShortHelp(const std::string& line) { 174 void PrintShortHelp(const std::string& line) {
175 EnsureInitialized();
176
139 size_t colon_offset = line.find(':'); 177 size_t colon_offset = line.find(':');
140 size_t first_normal = 0; 178 size_t first_normal = 0;
141 if (colon_offset != std::string::npos) { 179 if (colon_offset != std::string::npos) {
142 OutputString(" " + line.substr(0, colon_offset), DECORATION_YELLOW); 180 OutputString(" " + line.substr(0, colon_offset), DECORATION_YELLOW);
143 first_normal = colon_offset; 181 first_normal = colon_offset;
144 } 182 }
145 183
146 // See if the colon is followed by a " [" and if so, dim the contents of [ ]. 184 // See if the colon is followed by a " [" and if so, dim the contents of [ ].
147 if (first_normal > 0 && 185 if (first_normal > 0 &&
148 line.size() > first_normal + 2 && 186 line.size() > first_normal + 2 &&
149 line[first_normal + 1] == ' ' && line[first_normal + 2] == '[') { 187 line[first_normal + 1] == ' ' && line[first_normal + 2] == '[') {
150 size_t begin_bracket = first_normal + 2; 188 size_t begin_bracket = first_normal + 2;
151 OutputString(": "); 189 OutputString(": ");
152 first_normal = line.find(']', begin_bracket); 190 first_normal = line.find(']', begin_bracket);
153 if (first_normal == std::string::npos) 191 if (first_normal == std::string::npos)
154 first_normal = line.size(); 192 first_normal = line.size();
155 else 193 else
156 first_normal++; 194 first_normal++;
157 OutputString(line.substr(begin_bracket, first_normal - begin_bracket), 195 OutputString(line.substr(begin_bracket, first_normal - begin_bracket),
158 DECORATION_DIM); 196 DECORATION_DIM);
159 } 197 }
160 198
161 OutputString(line.substr(first_normal) + "\n"); 199 OutputString(line.substr(first_normal) + "\n");
162 } 200 }
163 201
164 void PrintLongHelp(const std::string& text) { 202 void PrintLongHelp(const std::string& text) {
203 EnsureInitialized();
204
165 std::vector<std::string> lines; 205 std::vector<std::string> lines;
166 base::SplitStringDontTrim(text, '\n', &lines); 206 base::SplitStringDontTrim(text, '\n', &lines);
167 207
208 bool first_header = true;
209 bool in_body = false;
168 for (const auto& line : lines) { 210 for (const auto& line : lines) {
169 // Check for a heading line. 211 // Check for a heading line.
170 if (!line.empty() && line[0] != ' ') { 212 if (!line.empty() && line[0] != ' ') {
213 if (is_markdown) {
214 // GN's block-level formatting is converted to markdown as follows:
215 // * The first heading is treated as an H2.
216 // * Subsequent heading are treated as H3s.
217 // * Any other text is wrapped in a code block and displayed as-is.
218 //
219 // Span-level formatting (the decorations) is converted inside
220 // OutputString().
221 if (in_body) {
222 OutputString("```\n\n", DECORATION_NONE);
223 in_body = false;
224 }
225
226 if (first_header) {
227 OutputString("## ", DECORATION_NONE);
228 first_header = false;
229 } else {
230 OutputString("### ", DECORATION_NONE);
231 }
232 }
233
171 // Highlight up to the colon (if any). 234 // Highlight up to the colon (if any).
172 size_t chars_to_highlight = line.find(':'); 235 size_t chars_to_highlight = line.find(':');
173 if (chars_to_highlight == std::string::npos) 236 if (chars_to_highlight == std::string::npos)
174 chars_to_highlight = line.size(); 237 chars_to_highlight = line.size();
238
175 OutputString(line.substr(0, chars_to_highlight), DECORATION_YELLOW); 239 OutputString(line.substr(0, chars_to_highlight), DECORATION_YELLOW);
176 OutputString(line.substr(chars_to_highlight) + "\n"); 240 OutputString(line.substr(chars_to_highlight) + "\n");
177 continue; 241 continue;
242 } else if (!line.empty() && !in_body) {
243 OutputString("```\n", DECORATION_NONE);
244 in_body = true;
178 } 245 }
179 246
180 // Check for a comment. 247 // Check for a comment.
181 TextDecoration dec = DECORATION_NONE; 248 TextDecoration dec = DECORATION_NONE;
182 for (const auto& elem : line) { 249 for (const auto& elem : line) {
183 if (elem == '#') { 250 if (elem == '#' && !is_markdown) {
184 // Got a comment, draw dimmed. 251 // Got a comment, draw dimmed.
185 dec = DECORATION_DIM; 252 dec = DECORATION_DIM;
186 break; 253 break;
187 } else if (elem != ' ') { 254 } else if (elem != ' ') {
188 break; 255 break;
189 } 256 }
190 } 257 }
191 258
192 OutputString(line + "\n", dec); 259 OutputString(line + "\n", dec);
193 } 260 }
261
262 if (is_markdown && in_body)
263 OutputString("\n```\n");
194 } 264 }
195 265
OLDNEW
« no previous file with comments | « tools/gn/docs/reference.md ('k') | tools/gn/switches.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698