Skip to content

Commit e468e72

Browse files
committed
tools/format-changelog: Upgrade line-unwrapping to handle nested lists
The `fmt` tool from GNU coreutils is very simple, and doesn't have the needed configuration knobs to be able to handle the nested lists that we now routinely use in changelog entries; it ends up mangling them. As a result, for many recent releases I've been doing the reformatting semi-manually, in Emacs: M-q to unwrap lines (after setting the text width to 999), and then a regexp find-and-replace to turn references `zulip#1234` into `#F1234`. It seems to be somewhat annoying to get Emacs to operate within a shell pipeline. But it's less annoying for Vim; and Vim's `gq` works just as well as Emacs's M-q, given the right config. So use that.
1 parent 690a0e5 commit e468e72

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

tools/format-changelog

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,45 @@ extract_highlights() {
3030
}
3131

3232
# Undo line-wrapping.
33+
_do_unwrap() {
34+
# Implemented by rewrapping paragraphs in Vim.
35+
# (Emacs works equally well, interactively; but seems more annoying
36+
# to integrate into a CLI pipeline.)
37+
38+
# The Vim "filetype" plugin for Markdown, relative to
39+
# the Vim runtime path (typically "/usr/share/vim/vim$version").
40+
# The file's upstream source is here:
41+
# https://github.com/tpope/vim-markdown/blob/f9f845f28/ftplugin/markdown.vim
42+
# The parts we actually use are the variables `comments`,
43+
# `formatoptions`, and `formatlistpat`.
44+
local vim_markdown_settings=ftplugin/markdown.vim
45+
46+
# The Vim command "gq" rewraps paragraphs; "G" makes it do so
47+
# from the current point to the end, and "gg" first goes to the start.
48+
vim -es /dev/stdin \
49+
"+runtime! ${vim_markdown_settings}" \
50+
'+set textwidth=2000' \
51+
'+normal gggqG' '+%print' '+:q!'
52+
}
53+
54+
# Undo line-wrapping; print an error if missing needed dependencies.
3355
unwrap() {
34-
fmt --tagged-paragraph --width=2000
56+
# A test string which gets rewrapped differently as plain text vs Markdown.
57+
local test_markdown=$'* a\n * b'
58+
if [ "$(_do_unwrap <<<"${test_markdown}")" != "${test_markdown}" ]; then
59+
# As plain text, the test string wraps to "* a * b".
60+
# But as Markdown, it's already wrapped, so shouldn't have changed.
61+
cat >&2 <<EOF
62+
error: Could not rewrap Markdown.
63+
Most likely either there is no \`vim\` command,
64+
or it is from a minimal Vim installation without the usual plugins.
65+
66+
Try installing Vim; e.g. on Debian, \`sudo apt install vim\`.
67+
EOF
68+
exit 1
69+
fi
70+
71+
_do_unwrap
3572
}
3673

3774
# Print changelog entry, with line-wrapping removed.

0 commit comments

Comments
 (0)