Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save fractaledmind/6573495 to your computer and use it in GitHub Desktop.

Select an option

Save fractaledmind/6573495 to your computer and use it in GitHub Desktop.
This is a DEMO, with a preset input. To use, change "t" to your input type. This is a Python script converts any Markdown style outline into a nested, numbered outline.
t = '- item 1\n\t- sub-item 1\n\t\t- sub-sub-item 1\n\t\t\t- sub-sub-sub-item 1\n\t\t- sub-sub-item 2\n\t- sub-item 2\n- item 2'
# replace tabs with 'x's
nt = t.replace('\t', 'x')
# create list from string
l = nt.split('\n')
# split list into (A) list of prefixes and (B) list of text
pre_l = []
text_l = []
for i in l:
w = i.split(' ')
pre = w[0]
pre_l.append(pre)
te = w[1:]
text = ' '.join(te)
text_l.append(text)
# prepare for numerical conversion
pre_l[0] = '1'
# convert prefixes to numbers
for i, item in enumerate(pre_l[:-1]):
x = len(pre_l[i])
y = len(pre_l[i + 1])
# if x < y, then [i + 1] is child of [i]
if x < y:
num = int(pre_l[i])
new = (num * 10) + 1
new = str(new)
pre_l[i + 1] = new
# if x = y, then [i] and [i + 1] are siblings
elif x == y:
num = int(pre_l[i])
new = (num + 1)
new = str(new)
pre_l[i + 1] = new
# if x > y, then [i + 1] is sibling of some preceeding item
elif x > y:
rev = pre_l[::-1]
r_in = (len(pre_l) - (i + 1))
rev = rev[r_in:]
for j, rev_item in enumerate(rev):
if len(rev[j]) == y:
pos = pre_l.index(rev[j])
num = int(pre_l[pos])
new = (num + 1)
new = str(new)
pre_l[i + 1] = new
break
# insert periods between multiple digit numbers
new_l = []
for t in pre_l:
z = '.'.join([t[i:i+1] for i in range(0, len(t), 1)])
new_l.append(z)
# add period after single digit items
li = []
for i, item in enumerate(new_l):
if len(item) == 1:
li.append(item)
li.append('.')
s = ''.join(li)
new_l[i] = s
li = []
# add tabs back to list
for i, item in enumerate(new_l):
# This asks if the length of the item is an odd number
if ((len(new_l[i]) > 2) & ((len(new_l[i]) % 2) == 1)):
# This rounds down the division of the odd number by 2, such that 3/2 = 1
tabs = len(new_l[i])/2
k = ''
for j in range(0, tabs):
k = k + '\t'
new_l[i] = k + item
#interlace prefix list and text list
grouped_l = zip(new_l, text_l)
# convert final list into string
for i in grouped_l:
x = ' '.join(i)
print x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment