Skip to content

Fix XML files indentation

Jacques Xing requested to merge fix/XML-format-indentation into master

Issue/feature addressed

Many xml files have trailing white space and tab/indentation issues. This affect readability of the xml files.

Proposed solution

The proposed solution is to use bash scripts to determine/correct the problematic files.

Implementation

The following bash script was use to fix indentation a new line


#!/bin/bash
for file in $(find -type f); do
    filename=${file##*/}
    if [[ "${filename##*.}" == "xml" ]]; then
        echo Formatting XML file: $file
        tab=""
        while read -r line || [[ -n "$line" ]]; do
            # Print empty line
            if [[ $line == "" ]]; then
                echo "" >> tmp
                continue
            fi

            # Just print line if comment
            if [[ $line == "<!--"*"-->" ]]; then
                echo "${tab}${line}" >> tmp
                continue
            fi

            # Remove tab
            if [[ $line == "</"*">"* ]] || [[ $line == "/>"* ]] ||
               [[ $line == "-->" ]]; then
                tab=${tab::-4}
            fi

            # Print line
            echo "${tab}${line}" >> tmp

            # Add tab
            if [[ $line == "<!--" ]] || [[ $line == "<!--"* ]]; then
                # Comment tag
                tab=${tab}"    "
            elif [[ $line == "<"* ]] && [[ $line == !("</"*) ]]; then
                # Opening tag
                if [[ $line == !("<?"*"?>") ]]; then
                    tab=${tab}"    "
                fi
            fi

            # Remove tab
            if  [[ $line == "-->" ]]; then
                # Comment tag
                continue
            elif  [[ $line == *"-->" ]] && [[ $line == !(*">"*"-->") ]] &&
                  [[ $line == !(*"</"*"-->") ]]; then
                tab=${tab::-4}
            elif [[ $line == "<"*"</"*">"* ]] || [[ $line == "<"*"/>"* ]] ||
             ( [[ $line == !("<"*) ]] && [[ $line == !("/>"*) ]] && [[ $line == *"/>"* ]] ); then
                # Closing tag
                tab=${tab::-4}
            fi

        done < $file

        # Rename file
        mv tmp $file
    fi
done

Also, some XML files have apparently been generated on Windows. The following script can be use to fix some formatting issues.

#!/bin/bash
for file in $(find -type f); do
    filename=${file##*/}
    if [[ "${filename##*.}" == "xml" ]]; then
        dos2unix $file
    fi
done

PS: The dos2unis formatting should be use before running the first script.

Checklist

  • Changelog is updated.

Warning

On the 19.07 the code formatting (code style) was standardised using clang-format, over the whole Nektar++ code. This means changes in your branch will conflict with formatting changes on the master branch. To resolve these conflicts , see #295 (closed)

Edited by Jacques Xing

Merge request reports