@@ -22,8 +22,173 @@ CLANG_FORMAT_EXTENSIONS="cc|h|proto"
2222
2323# Run clang-format.
2424find . -not -path " ./third_party/**" \
25- | egrep " \.(${CLANG_FORMAT_EXTENSIONS} )\$ " \
26- | xargs clang-format --verbose -style=google -i
25+ | egrep " \.(${CLANG_FORMAT_EXTENSIONS} )\$ " \
26+ | xargs clang-format --verbose -style=google -i
2727
2828bazel run -- \
29- @buildifier_prebuilt//:buildifier --lint=fix -r $( bazel info workspace)
29+ @buildifier_prebuilt//:buildifier --lint=fix -r $( bazel info workspace)
30+
31+ # =============================================================================
32+ # Format Markdown files according to Google's Markdown Style Guide.
33+ # https://google.github.io/styleguide/docguide/style.html
34+ # =============================================================================
35+
36+ format_markdown_file () {
37+ local file=" $1 "
38+ local tmp_file
39+ tmp_file=$( mktemp)
40+
41+ echo " Formatting Markdown: $file "
42+
43+ cp " $file " " $tmp_file "
44+
45+ # -------------------------------------------------------------------------
46+ # 1. TRAILING WHITESPACE
47+ # Remove trailing spaces/tabs from each line.
48+ # -------------------------------------------------------------------------
49+ sed -i ' s/[[:space:]]*$//' " $tmp_file "
50+
51+ # -------------------------------------------------------------------------
52+ # 2. ATX-STYLE HEADINGS
53+ # Convert setext-style headings to ATX style.
54+ # e.g. "Heading\n=======" -> "# Heading"
55+ # e.g. "Heading\n-------" -> "## Heading"
56+ # -------------------------------------------------------------------------
57+ awk '
58+ NR > 1 {
59+ prev_line = curr_line
60+ curr_line = $0
61+ if (curr_line ~ /^=+[[:space:]]*$/ && prev_line != "") {
62+ print "# " prev_line
63+ curr_line = ""
64+ prev_printed = 1
65+ next
66+ } else if (curr_line ~ /^-+[[:space:]]*$/ && prev_line != "") {
67+ print "## " prev_line
68+ curr_line = ""
69+ prev_printed = 1
70+ next
71+ } else {
72+ if (!prev_printed) print prev_line
73+ prev_printed = 0
74+ }
75+ next
76+ }
77+ NR == 1 { curr_line = $0; prev_printed = 0 }
78+ END { if (!prev_printed) print curr_line }
79+ ' " $tmp_file " > " ${tmp_file} .awk" && mv " ${tmp_file} .awk" " $tmp_file "
80+
81+ # -------------------------------------------------------------------------
82+ # 3. ADD SPACING TO HEADINGS
83+ # Ensure a space exists after # in headings.
84+ # e.g. "##Heading" -> "## Heading"
85+ # -------------------------------------------------------------------------
86+ sed -i ' s/^\(#\+\)\([^[:space:]#]\)/\1 \2/' " $tmp_file "
87+
88+ # -------------------------------------------------------------------------
89+ # 4. BLANK LINE BEFORE HEADINGS
90+ # Ensure there is a blank line before every heading.
91+ # -------------------------------------------------------------------------
92+ awk '
93+ /^#{1,6} / {
94+ if (NR > 1 && prev != "") print ""
95+ }
96+ { print; prev = $0 }
97+ ' " $tmp_file " > " ${tmp_file} .awk" && mv " ${tmp_file} .awk" " $tmp_file "
98+
99+ # -------------------------------------------------------------------------
100+ # 5. BLANK LINE AFTER HEADINGS
101+ # Ensure there is a blank line after every heading.
102+ # -------------------------------------------------------------------------
103+ awk '
104+ /^#{1,6} / {
105+ print
106+ getline next_line
107+ if (next_line != "") print ""
108+ print next_line
109+ next
110+ }
111+ { print }
112+ ' " $tmp_file " > " ${tmp_file} .awk" && mv " ${tmp_file} .awk" " $tmp_file "
113+
114+ # -------------------------------------------------------------------------
115+ # 6. FENCED CODE BLOCKS
116+ # Convert top-level indented code blocks (4 spaces) to fenced code blocks.
117+ # -------------------------------------------------------------------------
118+ awk '
119+ BEGIN { in_code = 0; in_list = 0 }
120+ /^[*\-] / || /^[0-9]+\. / { in_list = 1 }
121+ /^$/ { in_list = 0 }
122+ !in_list && /^ [^ ]/ && !in_code {
123+ print "```"
124+ sub(/^ /, "")
125+ print
126+ in_code = 1
127+ next
128+ }
129+ in_code && /^ / {
130+ sub(/^ /, "")
131+ print
132+ next
133+ }
134+ in_code && !/^ / {
135+ print "```"
136+ in_code = 0
137+ print
138+ next
139+ }
140+ { print }
141+ END { if (in_code) print "```" }
142+ ' " $tmp_file " > " ${tmp_file} .awk" && mv " ${tmp_file} .awk" " $tmp_file "
143+
144+ # -------------------------------------------------------------------------
145+ # 7. MULTIPLE BLANK LINES
146+ # Collapse multiple consecutive blank lines into a single blank line.
147+ # -------------------------------------------------------------------------
148+ cat -s " $tmp_file " > " ${tmp_file} .squeeze" && mv " ${tmp_file} .squeeze" " $tmp_file "
149+
150+ # -------------------------------------------------------------------------
151+ # 8. SINGLE NEWLINE AT END OF FILE
152+ # -------------------------------------------------------------------------
153+ awk ' BEGIN{RS=""; ORS="\n\n"} {gsub(/\n+$/, ""); print}' " $tmp_file " | \
154+ head -c -1 > " ${tmp_file} .eof"
155+ printf ' \n' >> " ${tmp_file} .eof"
156+ mv " ${tmp_file} .eof" " $tmp_file "
157+
158+ # -------------------------------------------------------------------------
159+ # 9. LINE LENGTH WARNING (80 chars)
160+ # Warn about lines exceeding 80 characters, excluding links, tables,
161+ # headings, and code blocks as per the style guide.
162+ # -------------------------------------------------------------------------
163+ local line_num=0
164+ local in_code_block=0
165+ while IFS= read -r line; do
166+ line_num=$(( line_num + 1 ))
167+ if [[ " $line " =~ ^\`\`\` ]]; then
168+ if [ $in_code_block -eq 0 ]; then
169+ in_code_block=1
170+ else
171+ in_code_block=0
172+ fi
173+ fi
174+ if [ $in_code_block -eq 0 ]; then
175+ if [[ ! " $line " =~ ^\# ]] && \
176+ [[ ! " $line " =~ \| ]] && \
177+ [[ ! " $line " =~ \]\( ]] && \
178+ [[ ! " $line " =~ \]\: ]]; then
179+ if [ ${# line} -gt 80 ]; then
180+ echo " WARNING: $file :$line_num exceeds 80 chars (${# line} chars)"
181+ fi
182+ fi
183+ fi
184+ done < " $tmp_file "
185+
186+ mv " $tmp_file " " $file "
187+ }
188+
189+ # Find and format all Markdown files, excluding third_party.
190+ echo " Formatting Markdown files..."
191+ find . -not -path " ./third_party/**" -name " *.md" -type f | while read -r file; do
192+ format_markdown_file " $file "
193+ done
194+ echo " Markdown formatting complete."
0 commit comments