| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- #
- # This script is a conveter helper for Obsiditan linter
- # to converter the orginal date field name
- # from "date created" to "date-created"
- # from "date modified" to "date-modified"
- # because the dataview plugin cannot identify the white space
- #
- # Created on: 2022-07-10
- # Author: Xing@deyisi.com
- import os
- import argparse
- def go_through_docs(work_dir):
- count = 0
- for root, dirs, files in os.walk(work_dir):
- for filename in files:
- if filename.endswith(".md"):
- if convert(os.path.join(root, filename)):
- count += 1
- print(f"{count:3d}: \"{filename}\" is converted")
- def change(line):
- if line.startswith("date created:"):
- return line.replace(
- "date created",
- "date-created"
- )
- converted = True
- if line.startswith("date modified:"):
- return line.replace(
- "date modified",
- "date-modified"
- )
- if line.startswith("date updated:"):
- return line.replace(
- "date updated",
- "date-updated"
- )
- def convert(filename):
- contents = []
- converted = False
- with open(filename, encoding="utf-8") as reader:
- yaml_entry = False
- line_no = 0
- for line in reader.readlines():
- if line_no == 0:
- if line == "---\n":
- yaml_entry = True
- if yaml_entry:
- if line == "---\n" and line_no > 0:
- yaml_entry = False
- else:
- updated = change(line)
- if updated is not None:
- line = updated
- converted = True
- contents.append(line)
- line_no += 1
- with open(filename, "w", encoding="utf-8") as writer:
- writer.write("".join(contents))
- return converted
- if __name__ == "__main__":
- parser = argparse.ArgumentParser(
- description="Converter existing Markdown documents with"
- " new linter format.\n"
- "The linter format `date created` will change to "
- "`date-created` format.",
- )
- parser.add_argument(
- '--dir',
- help='The location of the source ',
- default=os.getcwd(),
- )
- args = parser.parse_args()
- work_dir = args.dir
- print(f"Converting `{work_dir}` to new format...")
- if work_dir is None:
- work_dir = os.getcwd()
- if os.path.isdir(work_dir):
- go_through_docs(work_dir)
- else:
- print(f"Error: no such directory {work_dir} found.")
|