summaryrefslogtreecommitdiffstats
path: root/src/g2h/file.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/g2h/file.py')
-rw-r--r--src/g2h/file.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/g2h/file.py b/src/g2h/file.py
new file mode 100644
index 0000000..e07f251
--- /dev/null
+++ b/src/g2h/file.py
@@ -0,0 +1,27 @@
+import os
+
+def generate_breadcrumbs(content_subdir, commit, dir, files):
+ breadcrumbs = '[/](/{})'.format(os.path.join(content_subdir, tree_join(commit, '')))
+ subpath = ''
+ for segment in dir.split(os.sep):
+ if not segment or len(segment) == 0:
+ continue
+ subpath = os.path.join(subpath, sanitize_file_name(segment, files))
+ breadcrumbs += '/[{}](/{})'.format(segment, os.path.join(content_subdir, tree_join(commit, subpath)))
+ return breadcrumbs
+
+
+def sanitize_file_name(file_name, files):
+ if not file_name.startswith('.'):
+ return file_name
+
+ file_names = [ f.name for f in files ]
+ for i in range(1, 10):
+ new_name = file_name.replace('.', '_' * i, 1)
+ if not new_name in file_names:
+ return new_name
+
+ raise RuntimeError('Couldn not find unused sanitized file name')
+
+def tree_join(commit, path):
+ return os.path.join(commit.hexsha, 't', path)