FANDOM


(Change to TipImported template + severe manual clean)
(Merge comments with page, rm {{review}}, rm {{duplicate}} (other page put up for deletion))
Line 1: Line 1:
{{review}}
 
{{Duplicate|706}}
 
 
{{TipImported
 
{{TipImported
 
|id=1005
 
|id=1005
Line 7: Line 5:
 
|created=September 29, 2005
 
|created=September 29, 2005
 
|complexity=basic
 
|complexity=basic
|author=Jos van den Oever
+
|author=Jos van den Oever and others
 
|version=5.7
 
|version=5.7
 
|rating=6/5
 
|rating=6/5
 
}}
 
}}
  +
  +
There are several ways to deal with HTML entities.
  +
  +
== Simple search & replace ==
 
This code allows you to escape your HTML entities with one shortcut key: Change (<tt>&lt;, &gt;, &amp;</tt>) to (<tt>&amp;lt;, &amp;gt;, &amp;amp;</tt>), or the reverse.
 
This code allows you to escape your HTML entities with one shortcut key: Change (<tt>&lt;, &gt;, &amp;</tt>) to (<tt>&amp;lt;, &amp;gt;, &amp;amp;</tt>), or the reverse.
  +
  +
Note that this does not escape all characters that should be escaped, just the most common.
   
 
<pre>
 
<pre>
Line 26: Line 30:
 
endfunction
 
endfunction
   
vmap &lt;silent&gt; &lt;c-h&gt; :call HtmlEscape()&lt;CR&gt;
+
map &lt;silent&gt; &lt;c-h&gt; :call HtmlEscape()&lt;CR&gt;
vmap &lt;silent&gt; &lt;c-u&gt; :call HtmlUnEscape()&lt;CR&gt;
+
map &lt;silent&gt; &lt;c-u&gt; :call HtmlUnEscape()&lt;CR&gt;
 
</pre>
 
</pre>
   
 
If you add this code to your vimrc, you can escape visually-selected HTML with ctrl-h, and unescape with ctrl-u.
 
If you add this code to your vimrc, you can escape visually-selected HTML with ctrl-h, and unescape with ctrl-u.
   
==Comments==
+
==Automagic escaping==
Here's a script that does this for you automagically when you read and write files, so you can view the characters, and write the codes, or vice versa: {{script|id=909}}.
+
There's also script that does this for you automagically when you read and write files, so you can view the characters, and write the codes, or vice versa: {{script|id=909}}.
   
I did this originally for Java unicodes, but there is also a setting for html codes.
+
Originally written for Java unicodes, but there is also a setting for html codes.
   
 
The script is for &amp;nnn style encoding, not the html entities.
 
The script is for &amp;nnn style encoding, not the html entities.
   
----
+
==perl HTML::Entities==
I do it like this:
+
''' Does not seem to work with vim 7.1, maybe it can be fixed? '''
  +
  +
A slightly more complex solution that escape all characters is using perl, you will need [http://www.perl.org/ perl] and [http://search.cpan.org/dist/HTML-Parser/ HTML-Parser]
  +
 
<pre>
 
<pre>
 
function! HTMLEncode()
 
function! HTMLEncode()
Line 61: Line 65:
 
EOF
 
EOF
 
endfunction
 
endfunction
</pre>
 
   
This is quite extensible, for example LDAP BASE64 encoding:
 
<pre>
 
function! LdapEncode64()
 
perl &lt;&lt; EOF
 
use MIME::Base64;
 
@pos = $curwin-&gt;Cursor();
 
$line = $curbuf-&gt;Get($pos[0]);
 
($key) = ( $line =~ m/^([\w;_\-]+):/);
 
($value) = ( $line =~ m/^[\w;_\-]+:\s*(.*)/);
 
$encvalue = encode_base64($value);
 
$encvalue =~ s/[\r|\n]//g;
 
$curbuf-&gt;Set($pos[0],$key.":: ".$encvalue)
 
EOF
 
endfunction
 
 
function! LdapDecode64()
 
perl &lt;&lt; EOF
 
use MIME::Base64;
 
@pos = $curwin-&gt;Cursor();
 
$line = $curbuf-&gt;Get($pos[0]);
 
($key) = ( $line =~ m/^([\w;_\-]+)::/);
 
($value) = ( $line =~ m/^[\w;_\-]+::\s*(.*)/);
 
$decvalue = decode_base64($value);
 
$devvalue =~ s/[\r|\n]//g;
 
$curbuf-&gt;Set($pos[0],$key.": ".$decvalue)
 
EOF
 
endfunction
 
</pre>
 
 
You can map them like this:
 
<pre>
 
map &lt;Leader&gt;b :call LdapEncode64()&lt;CR&gt;
 
map &lt;Leader&gt;B :call LdapDecode64()&lt;CR&gt;
 
 
map &lt;Leader&gt;h :call HTMLEncode()&lt;CR&gt;
 
map &lt;Leader&gt;h :call HTMLEncode()&lt;CR&gt;
 
map &lt;Leader&gt;H :call HTMLDecode()&lt;CR&gt;
 
map &lt;Leader&gt;H :call HTMLDecode()&lt;CR&gt;
 
</pre>
 
</pre>
   
Go to the line and do <tt>\h</tt> or <tt>\H</tt> to check it out,
+
Go to the line and do <tt>\h</tt> or <tt>\H</tt> to check it out.
   
----
 
 
[[Category:HTML]]
 
[[Category:HTML]]

Revision as of 03:52, April 13, 2008

Tip 1005 Printable Monobook Previous Next

created September 29, 2005 · complexity basic · author Jos van den Oever and others · version 5.7


There are several ways to deal with HTML entities.

Simple search & replace

This code allows you to escape your HTML entities with one shortcut key: Change (<, >, &) to (&lt;, &gt;, &amp;), or the reverse.

Note that this does not escape all characters that should be escaped, just the most common.

function HtmlEscape()
  silent s/&/\&amp;/eg
  silent s/</\&lt;/eg
  silent s/>/\&gt;/eg
endfunction

function HtmlUnEscape()
  silent s/&lt;/</eg
  silent s/&gt;/>/eg
  silent s/&amp;/\&/eg
endfunction

map <silent> <c-h> :call HtmlEscape()<CR>
map <silent> <c-u> :call HtmlUnEscape()<CR>

If you add this code to your vimrc, you can escape visually-selected HTML with ctrl-h, and unescape with ctrl-u.

Automagic escaping

There's also script that does this for you automagically when you read and write files, so you can view the characters, and write the codes, or vice versa: script#909.

Originally written for Java unicodes, but there is also a setting for html codes.

The script is for &nnn style encoding, not the html entities.

perl HTML::Entities

Does not seem to work with vim 7.1, maybe it can be fixed?

A slightly more complex solution that escape all characters is using perl, you will need perl and HTML-Parser

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

map <Leader>h :call HTMLEncode()<CR>
map <Leader>H :call HTMLDecode()<CR>

Go to the line and do \h or \H to check it out.

Community content is available under CC-BY-SA unless otherwise noted.