Editorial Workflows

Shallower MD prefix

public workflow

Install Workflow...

This workflow contains at least one Python script. Only use it if you trust the person who shared this with you, and if you know exactly what it does.

I understand, install the workflow!

This is a workflow for Editorial, a Markdown and plain text editor for iOS. To download it, you need to view this page on a device that has the app installed.

Description: Reduces the hash-count, or tab-count, in headers and list items.
Subtracts four spaces from code lines, and a > from quotation lines.

At the margin (no prefix) toggles between body line and header

Shared by: Rob Trew – Twitter: @complexpoint

Comments: Comment Feed (RSS)

There are no comments yet.

+ Add Comment

Workflow Preview
Run Python Script ?
Source Code
#coding: utf-8
import editor
import re

RGX_PFX = """
^([\t]*)			# In some cases some tab indents,
	(						# then possibly a prefix:
	[\-\*\+]\s| 	# unordered list item
  \d+\.\s|			# ordered list item,
  \#+\ *|				# hash header,
  \>+\s*|			# block quote, or
  \s{4}|					# literal code
  )
  (.*)$       # and perhaps some text ...
"""

def outdented_prefix(o_match):
	str_prefix = o_match.group(2)
	lng_prefix = len(str_prefix.rstrip())
	
	if len(str_prefix) > 0:
		str_first = str_prefix[0]
		
		if str_first in ['#', '>']:
			lng_new_level = lng_prefix - 1
			if lng_new_level < 1:
				return ''
			else:
				return ''.join([str_first * lng_new_level,' '])
		elif str_first == ' ':
			return str_prefix[4:]
		else:
			# bullets with zero or more tabs
			lng_tabs = len(o_match.group(1))
			if lng_tabs > 0: # tab(s) found
				return ''.join([(lng_tabs-1) * '\t', str_prefix])
			else:
				return ''
	else:
		return "# "

tpl_seln = editor.get_selection()

# FULLY SELECT FROM START TO END OF SELECTED LINE(S)
tpl_lines	 = editor.get_line_selection()
editor.set_selection(tpl_lines[0], tpl_lines[1])
str_text = editor.get_selected_text()

lst_lines = str_text.splitlines()
bln_single_line = (len(lst_lines) < 2)

# INDENT EACH LINE, ACCORDING TO ITS TYPE
rgx_pfx = re.compile(RGX_PFX, re.VERBOSE)
lst_new = []
for str_line in lst_lines:
	o_match = rgx_pfx.match(str_line)
	if o_match != None:
		lst_new += [outdented_prefix(o_match) + o_match.group(3)]
		

if lst_new != []:
	str_new = '\n'.join(lst_new) + '\n'
	lng_new = len(str_new)
	lng_delta = len(str_text) - lng_new
	editor.replace_text(tpl_lines[0], tpl_lines[1], str_new)
else:
	str_new = str_text
	lng_delta = 0
	
lng_new = len(str_new)
		
# AND RESTORE FULL LINE SELECTIONS, FOR FURTHER INDENT/OUTDENT
editor.set_selection(tpl_lines[0], tpl_lines[1])
tpl_lines = editor.get_line_selection()
lng_from = tpl_lines[0]
if bln_single_line:
	lng_start, lng_end = tpl_seln[0]-lng_delta, tpl_seln[1]-lng_delta
	if lng_start < lng_from:
		lng_start = lng_from
	if lng_end < lng_from:
		lng_end = lng_from
	
	editor.set_selection(lng_start, lng_end)
else:
	editor.set_selection(lng_from, lng_from + lng_new-1)