<commit_before>#!/usr/bin/env coffee

coffeekup = require 'coffeekup'
fs = require 'fs'
path = require 'path'
puts = console.log
OptionParser = require('coffee-script/optparse').OptionParser

# On coffee-script@0.9.6, argv looks like [filename],
# On coffee-script@1.0.0, argv looks like ["node", "path/to/coffee", filename]
if process.argv[0] is 'node' and process.argv.length >= 2
  argv = process.argv[2..]
else
  argv = process.argv[0..]

render = (input_path) ->
  fs.readFile input_path, (err, contents) ->
    throw err if err
    html = coffeekup.render String(contents), options
    write input_path, html

write = (input_path, html) ->
  filename = path.basename(input_path, path.extname(input_path)) + '.html'
  dir = path.dirname input_path
  output_path = path.join dir, filename
  html = ' ' if html.length <= 0
  fs.writeFile output_path, html, (err) ->
    puts html if options.print
    puts "Compiled #{input_path}" if options.watch

usage = '''
  Usage:
    coffeekup [OPTIONS] path/to/template.coffee
'''

switches = [
  ['-w', '--watch', 'Keeps watching the file and recompiling it when it changes']
  ['-p', '--print', 'Prints the compiled html to stdout']
  ['-f', '--format', 'Applies line breaks to html output']
  ['-u', '--utils', 'Adds helper locals (currently only "render")']
  ['-h', '--help', 'Prints this help message']
  ['-v', '--version', 'Shows CoffeeKup version']
]

parser = new OptionParser switches, usage
options = parser.parse argv
args = options.arguments
delete options.arguments

puts parser.help() if options.help or argv.length is 0
puts coffeekup.version if options.version
if options.utils
  options.locals ?= {}
  options.locals.render = (file) ->
    contents = fs.readFileSync file
    coffeekup.render String(contents), options

if args.length > 0
  file = args[0]

  if options.watch
    fs.watchFile file, {persistent: true, interval: 500}, (curr, prev) ->
      return if curr.size is prev.size and curr.mtime.getTime() is prev.mtime.getTime()
      render file
  else render file<commit_msg>['-o', '--output [DIR]', 'Specifies a directory to compile into.']<commit_after>#!/usr/bin/env coffee

coffeekup = require 'coffeekup'
fs = require 'fs'
path = require 'path'
puts = console.log
OptionParser = require('coffee-script/optparse').OptionParser

# On coffee-script@0.9.6, argv looks like [filename],
# On coffee-script@1.0.0, argv looks like ["node", "path/to/coffee", filename]
if process.argv[0] is 'node' and process.argv.length >= 2
  argv = process.argv[2..]
else
  argv = process.argv[0..]

render = (input_path, output_directory) ->
  fs.readFile input_path, (err, contents) ->
    throw err if err
    html = coffeekup.render String(contents), options
    write input_path, html, output_directory

write = (input_path, html, output_directory) ->
  filename = path.basename(input_path, path.extname(input_path)) + '.html'
  dir = output_directory or path.dirname input_path
  path.exists dir, (exists) ->
    unless exists then fs.mkdirSync dir, 0777
    
    output_path = path.join dir, filename
    html = ' ' if html.length <= 0
    fs.writeFile output_path, html, (err) ->
      throw err if err
      puts html if options.print
      puts "Compiled #{input_path}" if options.watch

usage = '''
  Usage:
    coffeekup [OPTIONS] path/to/template.coffee
'''

switches = [
  ['-w', '--watch', 'Keeps watching the file and recompiling it when it changes']
  ['-o', '--output [DIR]', 'Specifies a directory to compile into.']
  ['-p', '--print', 'Prints the compiled html to stdout']
  ['-f', '--format', 'Applies line breaks to html output']
  ['-u', '--utils', 'Adds helper locals (currently only "render")']
  ['-h', '--help', 'Prints this help message']
  ['-v', '--version', 'Shows CoffeeKup version']
]

parser = new OptionParser switches, usage
options = parser.parse argv
args = options.arguments
delete options.arguments

puts parser.help() if options.help or argv.length is 0
puts coffeekup.version if options.version
if options.utils
  options.locals ?= {}
  options.locals.render = (file) ->
    contents = fs.readFileSync file
    coffeekup.render String(contents), options

if args.length > 0
  file = args[0]

  if options.watch
    fs.watchFile file, {persistent: true, interval: 500}, (curr, prev) ->
      return if curr.size is prev.size and curr.mtime.getTime() is prev.mtime.getTime()
      render file, options.output
  else render file, options.output
{'fraction_non_alphanumeric': 0.14298581400585453, 'fraction_numerical': 0.008556631389326727, 'mean_word_length': 3.720510095642933, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1736116', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1460, 'n_words': 448}