Last active
December 19, 2023 02:31
-
-
Save zhaochunqi/f9be10fe3d55e6684d05b157179e34b6 to your computer and use it in GitHub Desktop.
convert org to markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import subprocess | |
def run_bash_convert(file_path): | |
original_file = file_path | |
output_file = file_path.replace('.org', '.md') | |
process = subprocess.Popen(["mldoc", "convert", "-i", original_file, "-o", output_file]) | |
output, error = process.communicate() | |
print(output) | |
def count_lines(file_path): | |
original_file = file_path | |
output_file = file_path.replace('.org', '.md') | |
with open(original_file, 'r') as f: | |
original_file_lines = len(f.readlines()) | |
with open(output_file, 'r') as f: | |
output_file_lines = len(f.readlines()) | |
if original_file_lines < output_file_lines: | |
os.remove(original_file) | |
def check_dir_files(dir_path, ext): | |
for filename in os.listdir(dir_path): | |
f = os.path.join(dir_path, filename) | |
if os.path.isfile(f): | |
if f.endswith(ext): | |
run_bash_convert(f) | |
count_lines(f) | |
if os.path.isdir(f): | |
check_dir_files(f, ext) | |
def main(): | |
check_dir_files("./", '.org', ) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment