Skip to content

Instantly share code, notes, and snippets.

@rwilcox
Created August 2, 2012 13:21
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwilcox/3237070 to your computer and use it in GitHub Desktop.
Save rwilcox/3237070 to your computer and use it in GitHub Desktop.
Sends each line in the current BBEdit document to its own file
#!/usr/bin/env python
# A simple script that takes each line of the currently open (and saved!) document
# and outputs a file for each line, named with the first matching group of the name_grep
# regex
#
# It saves its output to a folder named "output" on the desktop
#
# Not really meant for general consumption, but as a tool that gets modified with every new
# time you have to do this.
#
# WD-rpw 07-31-2012
import fileinput
import re, os
OUTPUT_DIRECTORY = os.path.expanduser("~/Desktop/output")
name_grep = re.compile( r"(.+?)\(" ) # the grep used to derive the file name (group 1 is used for the name)
# this particular regex was meant to parse lines of function declarations and use the name of the function as the file name
# Like so:
#
# somefunction(paramaters)
if not os.path.exists(OUTPUT_DIRECTORY):
os.mkdir(OUTPUT_DIRECTORY)
for line in fileinput.input( [ os.environ.get("BB_DOC_PATH") ] ):
res = name_grep.search(line)
if res == None:
continue
filename = res.group(1)
with open( os.path.join(OUTPUT_DIRECTORY, filename), 'w' ) as f:
f.write( line )
print "all done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment