···
+
# This script is written intended as a living, evolving tooling
+
# to fix oopsies within the docbook documentation.
+
# This is *not* a formatter. It, instead, handles some known cases
+
# where something bad happened, and fixing it manually is tedious.
+
# Read the code to see the different cases it handles.
+
# ALWAYS `make format` after fixing with this!
+
# ALWAYS read the changes, this tool isn't yet proven to be always right.
+
require "rexml/document"
+
if ARGV.length < 1 then
+
$stderr.puts "Needs a filename."
+
doc = Document.new(File.open(filename))
+
# Fixing varnames having a sibling element without spacing.
+
# This is to fix an initial `xmlformat` issue where `term`
+
# would mangle as spaces.
+
# <term><varname>types.separatedString</varname><replaceable>sep</replaceable> <----
+
# Generates: types.separatedStringsep
+
# <varlistentry xml:id='fun-makeWrapper'>
+
# <function>makeWrapper</function><replaceable>executable</replaceable><replaceable>wrapperfile</replaceable><replaceable>args</replaceable> <----
+
# Generates: makeWrapperexecutablewrapperfileargs
+
# <option>--option</option><replaceable>name</replaceable><replaceable>value</replaceable> <-----
+
# Generates: --optionnamevalue
+
doc.elements.each("//varlistentry/term") do |term|
+
["varname", "function", "option", "replaceable"].each do |prev_name|
+
term.elements.each(prev_name) do |el|
+
el.next_element.name == "replaceable" and
+
el.next_sibling_node.class == Element
+
term.insert_after(el, Text.new(" "))
+
# <command>nixos-option</command>
+
# <option>-I</option><replaceable>path</replaceable> <------
+
doc.elements.each("//cmdsynopsis/arg") do |term|
+
["option", "replaceable"].each do |prev_name|
+
term.elements.each(prev_name) do |el|
+
el.next_element.name == "replaceable" and
+
el.next_sibling_node.class == Element
+
term.insert_after(el, Text.new(" "))
+
# <option>--profile-name</option>
+
# </group><replaceable>name</replaceable> <----
+
# Generates: [{--profile-name | -p }name]
+
doc.elements.each("//cmdsynopsis/arg") do |term|
+
["group"].each do |prev_name|
+
term.elements.each(prev_name) do |el|
+
el.next_element.name == "replaceable" and
+
el.next_sibling_node.class == Element
+
term.insert_after(el, Text.new(" "))
+
doc.context[:attribute_quote] = :quote
+
doc.write(output: File.open(filename, "w"))