#!/usr/bin/env php -n
<?php/**
* Export a wrapped representation of the words of a text.
*
* Optional parameters in a line that follows the text:
* l<size>. The column width. Default: 72.
* s<characters>. Characters to remove. Default: punctuation.
* o<asc|desc>. Sort order. Default: none.
*//*
# ***** BEGIN LICENSE BLOCK *****
#
# Copyright (C) 2010 Mehdi Kabab <http://pioupioum.fr/>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# ***** END LICENSE BLOCK ***** */if(version_compare(PHP_VERSION,'5.0.0','<')){
throw new Exception('You must have PHP >= 5.0.0!');}$lenght=72;// the default column width.$strip='\p{P}';// removes the ponctuation$sort=false;// no sorting$raw=file('php://filter/read=string.tolower/resource=php://stdin',
FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);if(isset($raw[1])){$args=array_pop($raw);if(preg_match('#^(?:\s{0,}(?:l(?<lenght>\d+)|s(?<strip>.*)|o(?<sort>asc|desc))\s{0,})+$#u',trim($args),$matches)){if(isset($matches['lenght'])&&$matches['lenght'])$lenght=(int)$matches['lenght'];if(isset($matches['strip'])&&$matches['strip'])$strip.='|'.$matches['strip'];if(isset($matches['sort'])&&$matches['sort'])$sort=$matches['sort'];}else{array_push($raw,$args);}}$raw=implode(' ',$raw);// Removes punctuation and user charactersif($strip){$strip=str_replace('#','\#',$strip);$raw=preg_replace('#('.$strip.')#u','',$raw);}// Estimating indent$start_index=0;if(isset($_SERVER['TM_INPUT_START_LINE_INDEX'])){$start_index=$_SERVER['TM_INPUT_START_LINE_INDEX'];}if('YES'===$_SERVER['TM_SOFT_TABS']){$tab=str_repeat(' ',$_SERVER['TM_TAB_SIZE']);}else{$tab="\t";}$tab_count=floor($start_index/$_SERVER['TM_TAB_SIZE']);$extra_spaces=$start_index%$_SERVER['TM_TAB_SIZE'];$tabs=str_repeat($tab,$tab_count).str_repeat(' ',$extra_spaces);$tabs_len=$tab_count*$_SERVER['TM_TAB_SIZE']+$extra_spaces;// Extracting words now!$words=preg_split('#\s+#',$raw);$words=array_map('trim',$words);$words=array_filter(array_unique($words));// removes empty fieldsif($sort){natsort($words);if('desc'===$sort){$words=array_reverse($words);}}$words=sprintf('%s\'%s\'',$tabs,wordwrap(implode("', '",$words),$lenght-$tabs_len,"\n$tabs"));if(0!==$start_index){$words=ltrim($words);}if("''"===$words){return;}echo$words;