Skip to content

Instantly share code, notes, and snippets.

@ofca
Created November 14, 2013 00:11
Show Gist options
  • Save ofca/7458950 to your computer and use it in GitHub Desktop.
Save ofca/7458950 to your computer and use it in GitHub Desktop.
This script will fix docstrings in javascript files compiled by CoffeeScript.
from optparse import OptionParser
import pprint
import os
import sys
import re
parser = OptionParser(usage="This script will fix docstrings in javascript files compiled by CoffeeScript.")
parser.add_option('-s', '--src', help='source directory')
(options, args) = parser.parse_args()
# If source directory specified
if options.src:
source_path = options.src
# Scan directory
for root, subs, files in os.walk(source_path):
for filename in files:
name, ext = os.path.splitext(filename)
# Look for js files which has .coffee equivalent
if ext == '.js' and os.path.isfile(os.path.join(root, name+'.coffee')):
path = os.path.join(root, filename)
with open(path) as f:
lines = f.readlines()
opened = False
just_closed = False
spaces_num = 0
content = ''
for line in lines:
# Remove empty line after docstring
if just_closed:
just_closed = False
if bool(re.search('^\s*$', line)):
continue
# We are in docstring
elif opened:
# Append missing space to closing docstring tag
if bool(re.search('^\s*\*\/\s*$', line)):
opened = False
just_closed = True
line = line.replace('*/', ' */')
# Append ' * ' to comment line
else:
left = line[:spaces_num]
right = line[spaces_num:]
line = '%s * %s' % (left, right)
# Change /* to /**
elif bool(re.search('^\s*\/\*\s*$', line)):
opened = True
spaces_num = line.find('/')
line = line.replace('/*', '/**')
content += line
# Save changes to file
jsfile = open(path, 'w')
jsfile.write(content)
jsfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment