Monday, February 13, 2012

hope google api's page starts hosting WYMeditor

WYMeditor is a wonderful "web-based WYSIWYM (What You See Is What You Mean) XHTML editor (not WYSIWYG)which can be included as any textarea input of an html form, using nothing but java script. it has already become the standard editor in many open source CMS solutions.

if you are designing/developing a web page, and including a textarea form, and you want your users to be able to include (XHTML) formatted input,  i highly suggest using WYMeditor

any how, the main argument of my post:
     google hosts many open source javascript projects/frameworks/libraries on the google api's page. this is great for including the most recent version of scriptaculous, jquerry, ect while your designing a website without needing to host, or update the js files yourself.

it does look like you could currently hotlink the creator's .js scripts from the MYMeditor's files page, but it would be better if the google api's page hosts it (using google's server resources).

Saturday, February 11, 2012

Mixed language (bash/python) function to deal with stdin pipes

often i find the need to be able to use one of my bash scripts to process data from a stdin pipe like:

somecommand |myscript


the standard way is to 'cat /dev/stdin' but alone doesnt do what is needed. for instance perhaps you are not piping output to the script (however this is simple to deal with as an 'if ! [ -t 0 ] ' statement). sometimes options/argument can get mixed into the pipe. the standard solution is to read (ie: 'cat /dev/stdin') using a non-breaking(/blocking, i forget) stty, but this kind of reading of stdin is tricky, and confusing, and /dev/stdin may not be available in all systems (chrooted hosting accounts for instance).

a simple solution is to create a bash function calling python (most systems that include bash also include python) to read stdin, and write directly back to stdout (in essence a simple python pager called as a bash function). this function looks like:

getstdin (){
python -c "from sys import *
if not stdin.isatty():stdout.write(stdin.read())
else:exit(1)"
}


note: ';else:exit(1)' isnt strictly required, but gives a standard error code if nothing on the stdin pipe, which is useful to further script logic (determining the error codes of commands to decide what functionality to produce)

this function takes care of the previously mentioned method's pitfalls. include this function in your own bash script, and deal with (or ignore) stdin pipe easily. a simple example bash script using this function:

#!/bin/bash
#  mystdin.sh
#  by - nairb <c0d3@nairb.us>
#  purpose: to add the text "RECIEVED: " and return stdin pipe

getstdin (){
python -c "from sys import *
if not stdin.isatty():stdout.write(stdin.read())
else:exit(1)"
}

stdin=$(getstdin)
if [ $stdin ];then
  echo "RECEIVED: $stdin"
else
  echo "didnt get anything on stdin, doing something else"
fi


you could run the above example script with a command like:

echo "this is some text" | bash mystdin.sh


the above function is also an example of the standard way to read from a stdin pipe in python scripts (not just calling python in bash).

--
hope someone finds this post useful,
- nairb