As of 2016-02-26, there will be no more posts for this blog. s/blog/pba/
Showing posts with label escape. Show all posts

When I blog, sometimes I write in reStructuredText if the post is about technical topic involving programming codes, otherwise I just write in Blogger's editor.

Oftentimes, I was lazy when the post is very short like this one you are reading, so Blogger's editor was. If there is some HTML code or code like a >= b, I'll have to use escape tool or manually escape by hand.

Since I use Pentadactyl (fork of Vimperator), by pressing Ctrl+I, I can use external editor Vim when I need to do some re-indentation or mass replacement using regular expression. If you have done some reviewing on Stack Overflow, you definitely need to take care of a lot re-indentation, some code's spaces or tabs were eaten randomly, mysteriously, by the posters.

I adapted a code from Vim wiki, so I can also do escaping.
" ==========================================================================
" HTML Encode/Decode
" ==========================================================================
" ref: http://vim.wikia.com/wiki/HTML_entities#Perl_HTML::Entities
function! HTMLEncode()
perl << EOF
 use HTML::Entities;
 @pos = $curwin->Cursor();
 $line = $curbuf->Get($pos[0]);
 $encvalue = encode_entities($line);
 $curbuf->Set($pos[0],$encvalue)
EOF
endfunction

function! HTMLDecode()
perl << EOF
 use HTML::Entities;
 @pos = $curwin->Cursor();
 $line = $curbuf->Get($pos[0]);
 $encvalue = decode_entities($line);
 $curbuf->Set($pos[0],$encvalue)
EOF
endfunction

vmap <Leader>h :call HTMLEncode()<CR>
vmap <Leader>H :call HTMLDecode()<CR>
Only difference is the key mapping, it's most likely I only use escaping in Visual mode, because the code needs to be escaped is only a part of post. Certainly, I would select the part and escape them. If I need to escape the whole file, I can just type :%call HTMLEncode() or select all and use the key mapping.

Blogger is fine with some situations with characters like < or >, but definitely not HTML or XML element tag, you will have to escape them or it may think you actually want to use those HTML for your post.

And, yes, I know there is an option to tell Blogger not to interpret HTML code. But who uses that option, I would guess it's someone who doesn't even know what HTML is or never need to paste code into their posts. I am a programmer and I know what I want. For me, if I need italics style, most times, I wrap the text in <i/> by hand, it's much faster than moving mouse cursor to the button.

When I am writing blog posts using reStructuredText, I have F5 bound to running an external script with current filename using %. The external script generates temporary file with template, which I can preview in browser with. The post title is extracted from the filename.

Inevitably, characters like \' or ! are used sometimes. If you have used shell long enough, you will know those characters may cause issues if they are not quoted or escaped correctly.

Since I am just a normal Vim user, not really have much knowledge and experience of coding for Vim configuration. But this time, I managed to put together stuff to get away from escaping issue.

My original keybinding is as the following:

map <F5> <ESC>:w<CR>:!gen-blog-rst.sh '%'<CR>

It saves the file, then run the gen-blog-rst.sh script. As you can see \'%\' definitely will have issues with \' character. I found out there is a Vim function shellescape() which does what I expect:

:echo shellescape("\"blah\" 'foorbar' blah!")
" outputs the following string
'"blah" '\''foorbar'\'' blah!'

You can safely pass to :!some_command as an argument. It will be like running (my default shell is Bash):

bash -c some_command '"blah" '\''foorbar'\'' blah!'

This some_command will get that long word from $1.

The problem is when using :!{cmd}, I can not put anything function in it, it is basically a plain string be passed to your default shell. I don’t know if Vim has something like Command Substitution in Bash to work in string, if it does, let me know.

The solution I found for this part is to use :exec[ute] to wrap it up:

:exec '!gen-blog-rst.sh ' . shellescape(@%)

So, the final map is:

map <F5> <ESC>:w<CR>:exec '!gen-blog-rst.sh ' . shellescape(@%)<CR>

Note that @% is used for the function, instead of just %. I used this to update my vimrc.