about summary refs log tree commit diff stats
path: root/ranger/ext/trim.py
diff options
context:
space:
mode:
Diffstat (limited to 'ranger/ext/trim.py')
-rw-r--r--ranger/ext/trim.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/ranger/ext/trim.py b/ranger/ext/trim.py
new file mode 100644
index 00000000..d469435d
--- /dev/null
+++ b/ranger/ext/trim.py
@@ -0,0 +1,31 @@
+import sys
+
+def trim_docstring(docstring):
+	if not docstring:
+		return ''
+	return '\n'.join(trimmed_lines_of_docstring(docstring))
+
+def trimmed_lines_of_docstring(docstring):
+	if not docstring:
+		return []
+	# Convert tabs to spaces (following the normal Python rules)
+	# and split into a list of lines:
+	lines = docstring.expandtabs().splitlines()
+	# Determine minimum indentation (first line doesn't count):
+	indent = sys.maxint
+	for line in lines[1:]:
+		stripped = line.lstrip()
+		if stripped:
+			indent = min(indent, len(line) - len(stripped))
+	# Remove indentation (first line is special):
+	trimmed = [lines[0].strip()]
+	if indent < sys.maxint:
+		for line in lines[1:]:
+			trimmed.append(line[indent:].rstrip())
+	# Strip off trailing and leading blank lines:
+	while trimmed and not trimmed[-1]:
+		trimmed.pop()
+	while trimmed and not trimmed[0]:
+		trimmed.pop(0)
+	# Return a single string:
+	return trimmed