Skip to content

Instantly share code, notes, and snippets.

@q0rban
Created October 27, 2011 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save q0rban/1320043 to your computer and use it in GitHub Desktop.
Save q0rban/1320043 to your computer and use it in GitHub Desktop.
PHP Script for Coda to wrap to 80 Chars
#! /usr/bin/php
<?php
$string = getenv('CODA_SELECTED_TEXT');
function gimmee_spaces($length) {
return str_pad('', $length);
}
$length = strlen($string);
$cut = ltrim($string, " \t");
$cut_length = strlen($cut);
$pad = '';
$pad_length = $length - $cut_length;
if ($pad_length) {
$pad = gimmee_spaces($pad_length);
}
$prefixes = array('*', '//', ';', '#');
foreach ($prefixes as $prefix) {
if (strpos($cut, $prefix) === 0) {
$prefix_length = strlen($prefix);
$cut = ltrim(substr($cut, $prefix_length), " \t");
$new_cut_length = strlen($cut);
$pad .= $prefix;
$pad .= gimmee_spaces($cut_length - $prefix_length - $new_cut_length);
$cut_length = $new_cut_length;
}
}
$pad_length = $length - $cut_length;
$wrap_length = 80 - $pad_length;
$break = "\n$pad";
$output = wordwrap($cut, $wrap_length, $break);
print $pad . $output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment