Editorial Workflows

Affix

unlisted 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: This workflow has no description.

Shared by: @hals_pg

Comments: Comment Feed (RSS)

There are no comments yet.

+ Add Comment

Workflow Preview
Request Text Input ?
Title
1st: Prefix | 2nd: Suffix
Initial Text
{1}.
  • Single Line
  • Multiple Lines
Keyboard Options:
Set Variable ?
Variable Name
affix
Value
Input
Selected Text ?
Entire Line(s)
OFF
Empty Selection Output
  • No Output
  • All Text
  • Closest Word
Folded Text
  • Include
  • Replace with:
Run Python Script ?
Source Code
# -*- coding: utf-8 -*-
import sys
import workflow
import console
import re


sys.setdefaultencoding('utf-8')
source = workflow.get_variable('affix')

if source != '':
	try:
		prefix, suffix = source.split('\n')
	except:
		prefix = source
		suffix = ''
else:
	console.hud_alert('Empty input', 'error', 1.0)
	workflow.stop()

source = workflow.get_input().split('\n')
output = []
macro = re.compile(r'[^\d]*(\{(\d+)\})')

pre_macro = macro.match(prefix)
suf_macro = macro.match(suffix)

if pre_macro is not None:

	if suf_macro is not None:
		p = int(pre_macro.group(2))
		s = int(suf_macro.group(2))

		for line in source:
			output.append(
				prefix.replace(pre_macro.group(1), str(p)) + line + suffix.replace(suf_macro.group(1), str(s))
			)
			p = p + 1
			s = s + 1

	else:
		p = int(pre_macro.group(2))

		for line in source:
			output.append(
				prefix.replace(pre_macro.group(1), str(p)) + line + suffix
			)
			p = p + 1

elif suf_macro is not None:
	s = int(suf_macro.group(2))

	for line in source:
		output.append(
			prefix + line + suffix.replace(suf_macro.group(1), str(s))
		)
		s = s + 1

else:
	for line in source:
		output.append(
			prefix + line + suffix
		)

workflow.set_output('\n'.join(output))
Replace Selected Text ?
Replacement Text
Input