Skip to content

Instantly share code, notes, and snippets.

@kencoba
Last active May 12, 2017 04:38
Show Gist options
  • Save kencoba/b0069d11c5b85feedf0d7c943625ed80 to your computer and use it in GitHub Desktop.
Save kencoba/b0069d11c5b85feedf0d7c943625ed80 to your computer and use it in GitHub Desktop.
Add header number to markdown file.
# Markdown_AddHeaderNumber
# input: Markdown.md
# output: Markdown.md added number on headers
#
# header format
#
# ```Markdown
# # 1. header
# ## 1.1. header
# ```
BEGIN {
h1 = 0
h2 = 0
h3 = 0
h4 = 0
h5 = 0
h6 = 0
h7 = 0
number = /[0-9\.]+/
}
$1 == "#" && $2 ~ /[0-9.]+/ {
$2 = ++h1 "."
print
h2 = 0; h3 = 0; h4 = 0; h5 = 0; h6 = 0; h7 = 0;
}
$1 == "##" && $2 ~ /[0-9.]+/ {
$2 = h1 "." ++h2
print
h3 = 0; h4 = 0; h5 = 0; h6 = 0; h7 = 0;
}
$1 == "###" && $2 ~ /[0-9.]+/ {
$2 = h1 "." h2 "." ++h3
print
h4 = 0; h5 = 0; h6 = 0; h7 = 0;
}
$1 == "####" && $2 ~ /[0-9.]+/ {
$2 = h1 "." h2 "." h3 "." ++h4
print
h5 = 0; h6 = 0; h7 = 0;
}
$1 == "#####" && $2 ~ /[0-9.]+/ {
$2 = h1 "." h2 "." h3 "." h4 "." ++h5
print
h6 = 0; h7 = 0;
}
$1 == "######" && $2 ~ /[0-9.]+/ {
$2 = h1 "." h2 "." h3 "." h4 "." h5 "." ++h6
print
h7 = 0;
}
$1 == "#######" && $2 ~ /[0-9.]+/ {
$2 = h1 "." h2 "." h3 "." h4 "." h5 "." h6 "." ++h7
print
}
$1 ~ /[^#]+/
$0 == ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment