show non-plaintext files

This commit is contained in:
Akbar Rahman 2022-11-11 12:23:21 +00:00
parent 8e09404520
commit b050b36747
Signed by: alvierahman90
GPG Key ID: 20609519444A1269

View File

@ -22,6 +22,9 @@ INDEX_TEMPLATE_HEAD = None
EXTRA_INDEX_CONTENT = None EXTRA_INDEX_CONTENT = None
def is_plaintext(filename):
return re.match(r'^text/', magic.from_file(str(filename), mime=True)) is not None
def get_files(folder): def get_files(folder):
markdown = [] markdown = []
plaintext = [] plaintext = []
@ -34,7 +37,7 @@ def get_files(folder):
name = os.path.join(root, filename) name = os.path.join(root, filename)
if pathlib.Path(name).suffix == '.md': if pathlib.Path(name).suffix == '.md':
markdown.append(name) markdown.append(name)
elif re.match(r'^text/', magic.from_file(name, mime=True)): elif is_plaintext(name):
plaintext.append(name) plaintext.append(name)
other.append(name) other.append(name)
else: else:
@ -283,10 +286,11 @@ def main(args):
pathlib.Path(filename).relative_to(args.notes) pathlib.Path(filename).relative_to(args.notes)
) )
) )
title = os.path.basename(filename)
pathlib.Path(output_filename).parent.mkdir(parents=True, exist_ok=True) pathlib.Path(output_filename).parent.mkdir(parents=True, exist_ok=True)
all_entries.append({ all_entries.append({
'path': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])), 'path': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])),
'title': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])), 'title': title,
'tags': [get_inherited_tags(filename, args.notes)], 'tags': [get_inherited_tags(filename, args.notes)],
'headers': [] 'headers': []
}) })
@ -331,6 +335,7 @@ def main(args):
continue continue
fullpath = directory.joinpath(path) fullpath = directory.joinpath(path)
title = path.name
if path.suffix == '.html': if path.suffix == '.html':
with open(fullpath) as fp: with open(fullpath) as fp:
soup = bs(fp.read(), 'html.parser') soup = bs(fp.read(), 'html.parser')
@ -341,7 +346,7 @@ def main(args):
title = pathlib.Path(path).stem title = pathlib.Path(path).stem
elif fullpath.is_dir(): elif fullpath.is_dir():
title = path title = path
else: elif is_plaintext(fullpath):
# don't add plaintext files to index, since they have a html wrapper # don't add plaintext files to index, since they have a html wrapper
continue continue
@ -350,7 +355,7 @@ def main(args):
indexentries.append({ indexentries.append({
'title': str(title), 'title': str(title),
'path': str(path), 'path': './' + str(path),
'isdirectory': fullpath.is_dir() 'isdirectory': fullpath.is_dir()
}) })