OpenTTD-China-Set / China-Set-Trains

The Train sector of the China Set of OpenTTD.
GNU General Public License v2.0
12 stars 6 forks source link

Codechange: remove all //begin and //end comments in pnml files #58

Open WenSimEHRP opened 3 months ago

WenSimEHRP commented 3 months ago

Those comments are unnecessary. Their only use case is when they're compiled into one single NML file and the reader can use them to determine where each file starts and ends. However GCC can output the start and end in the form of "#" comments and it can do the job better (i.e. readable by NMLC). So here is a PR to remove all of them.

Script used

import glob
import re

# get all files recursively
files = glob.glob('**/*.pnml', recursive=True)

class RemoveHeader:
    def __init__(self, path: str):
        self.path = path

    def remove(self):
        with open(self.path, "r", encoding="utf-8-sig") as file:
            newfile = []
            for line in file:
                if re.match(r"//\s*?(begin|end)\s.*$", line.lower()):
                    line = ""
                newfile.append(line)
        with open(self.path, "w", encoding="utf-8") as file:
            file.writelines(newfile)

for file in files:
    rm = RemoveHeader(file)
    rm.remove()

Note that This PR might confilct with the previous PR that repaced vehicle ids.