Tuesday 30 August 2011

vim misc

copy from vim to vim command line:
yank text then
:^r"

execute yanked vim code
yank code then
:@"

" is register

display hex, octal ... of character under cursor
ga

display special characters
:digraphs

insert special character
press ctlr+k plus key from ":digraphs" in insert mode

Tuesday 26 July 2011

PLEAC

I found nice project that tries to create ebooks (html pages). These ebooks are copy of Perl Cookbook but using different programming languates:

http://pleac.sourceforge.net/

Friday 8 July 2011

Apache Velocity

Velocity consists of two main parts, Template and Context. Where template holds html, xml etc. and references, directives etc. Context holds the data.


Using Strig as template:

Context ctx = new VelocityContext();
ctx.put("user", "Admin user");

StringWriter writer = new StringWriter();
/* used as the template name for log messages in case of error */
String logTag = "template name";
String templateString = "<div>$user</div>";

Velocity.evaluate(ctx, writer, logTag, templateString);
System.out.println(writer.getBuffer());

Using Velocity Template

RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
Template template = new Template();
template.setRuntimeServices(runtimeServices);

StringReader reader = new StringReader("<div>$user</div>");
SimpleNode node = runtimeServices.parse(reader, "template name");
template.setData(node);
template.initDocument();

// ... create context and writer

template.merge(context, writer);

Merge method (template.merge(context, writer)) calls render method on its data (template.setData(node)), which is instance of SimpleNode.

SimpleNode then calls merge method on every child it has (SimpleNode or subclass of SimpleNode). Every part of the template (text, references, directives etc.) are represented as SimpleNode (or its subclass).

Knowing this we can for example count number of references (instances of ASTReference) in the template:


SimpleNode simpleNode = (SimpleNode) template.getData();

for (int x = 0; x < simpleNode.jjtGetNumChildren(); x++) {
    if (simpleNode.jjtGetChild(x) instanceof ASTReference) {
        ASTReference astRef = (ASTReference) simpleNode.jjtGetChild(x);
        // root reference without $, so '$user.name' becomes 'user'
        System.out.println(astRef.getRootString());
        // literal reference, so '$user.name' is '$user.name'
        System.out.println(astRef.literal());
    }
}

Loading resource from classpath

The following code loads resource (in this case spring macro).

StringBuilder springMacro = new StringBuilder();
ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
Scanner sc = new Scanner(resourceLoader.getResourceStream(
        "org/springframework/web/servlet/view/velocity/spring.vm"));
while(sc.hasNextLine()) {
    springMacro.append(sc.nextLine());
}

System.out.println(springMacro.toString());

This is needed only when using spring macro (spring.vm), so it has requestContext reference (needed for binding - #springBind(...))


VelocityContext context = new VelocityContext();
// request is instance of HttpServletRequest and mav is instance of ModelAndView
context.put("springMacroRequestContext", new RequestContext(request, mav.getModel()));

Using Velocity Tools


With standalone velocity, you can configure tools (tools.xml) this way:


ToolManager manager = new ToolManager();
/* path to tools.xml */
manager.configure("/path/to/my/configuration.xml");
Context context = manager.createContext();
myVelocityEngine.evaluate(context, myOutputWriter, 
        "This is a $text.test", "Test template");

Links:


Developer guide:
http://velocity.apache.org/engine/devel/developer-guide.html

User guide:
http://velocity.apache.org/engine/releases/velocity-1.6.4/user-guide.html

API:
http://velocity.apache.org/engine/releases/velocity-1.6.4/apidocs/index.html

Wednesday 29 September 2010

Git setup

user: 
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

editor:
$ git config --global core.editor emacs
 
diff tool:
$ git config --global merge.tool vimdiff
 
list your settings
$ git config --list 
or check specific value
$ git config user.name

help:
$ git help <verb>
$ git <verb> --help
$ man git-<verb>  
example: 
$ git help config 

source: http://progit.org/book/ch1-5.html

Wednesday 22 September 2010

PHP Environment setup - Netbeans, PHPUnit and xdebug

- filenames (paths) will be different for every platform/version
wamp server
Install wamp:
http://www.wampserver.com/en/

pear
open command line, go to:
C:\wamp\bin\php\php5.3.0
execute: 
php -d phar.require_hash=0 PEAR/go-pear.phar
and choose all defualt options (first option)

xdebug
start wamp sever and enable php curl extension

copy php info output and paste it here:
http://www.xdebug.org/find-binary.php
it shows you what xdebug version to download and where to put it

modify both php.ini (c:\wamp\bin\php\php5.3.0\php.ini and c:\wamp\bin\apache\Apache2.2.11\bin\php.ini)
add these lines at the end:

zend_extension = c:\wamp\bin\php\php5.3.0\ext\php_xdebug-2.1.0-5.3-vc6.dll
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_host=localhost
xdebug.remote_port=9000

your xdebug version could be different
restart apache server

netbeans
download and install netbeans (php)
http://netbeans.org/downloads/

phpunit
type on command line:
pear upgrade pear
pear channel-discover pear.phpunit.de
pear channel-discover pear.symfony-project.com
pear install phpunit/PHPUnit

make sure that you included PEAR in both php.ini:
include_path = ".;c:\wamp\bin\php\php5.3.0\PEAR"

restart apache

integrate with netbeans
in netbeans go to:
Tools -> Options -> PHP

under general tab, PHP 5 Interpreter:
C:\wamp\bin\php\php5.3.0\php.exe
Debugger port: 9000
Session ID: netbeans-xdebug

Under Unit Testing tab:
PHPUnit script: C:\wamp\bin\php\php5.3.0\phpunit.bat

vimrc

general vimrc file

" indent
set autoindent
set smartindent

" spaces instead of tab
set tabstop=4
set shiftwidth=4

" briefly jumps to the matching brace/bracket
set showmatch

" flashes the screen when the command does not work instead of beep
set vb t_vb=

set ruler " show the cursor position all the time
set scrolloff=3 " keep 3 lines when scrolling
syntax on " syntax highlighing

" font and color
set guifont=Courier_New
colorscheme koehler

" turn backup off
set nobackup

" line numbers
:set nu