Fixed textmate_import.rb slightly and added doc for it

This commit is contained in:
capitaomorte 2009-08-26 13:06:00 +00:00
parent 81857aea6d
commit 01205f79d6
5 changed files with 130 additions and 42 deletions

View File

@ -3,7 +3,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" /> <meta name="generator" content="Docutils 0.6: http://docutils.sourceforge.net/" />
<title>Yet Another Snippet extension</title> <title>Yet Another Snippet extension</title>
<link rel="stylesheet" href="styles.css" type="text/css" /> <link rel="stylesheet" href="styles.css" type="text/css" />
</head> </head>
@ -60,14 +60,14 @@
<li><a class="reference internal" href="#bugs-contribution-and-support" id="id6">Bugs, Contribution and Support</a></li> <li><a class="reference internal" href="#bugs-contribution-and-support" id="id6">Bugs, Contribution and Support</a></li>
</ul> </ul>
</div> </div>
<p>YASnippet is a template system for Emacs. It allows you to type a <p><strong>YASnippet</strong> is a template system for Emacs. It allows you to type a
abbreviation and automatically expand the abbreviation into function abbreviation and automatically expand the abbreviation into function
templates.</p> templates.</p>
<p>Bundled language templates includes: C, C++, C#, Perl, Python, Ruby, <p>Bundled language templates includes: C, C++, C#, Perl, Python, Ruby,
SQL, LaTeX, HTML, CSS and more.</p> SQL, LaTeX, HTML, CSS and more.</p>
<p>YASnippet system is inspired from TextMate's template system. You can <p>The snippet syntax is inspired from TextMate's syntax, you can
use <a class="reference external" href="https://groups.google.com/group/smart-snippet/browse_thread/thread/691fbdd33412d86e?pli=1">a tool</a> even <a class="reference external" href="snippet-development.html#importing-textmate-snippets">import</a>
to import any TextMate template you have to YASnippet.</p> import most TextMate templates to YASnippet.</p>
<p>YASnippet is a re-write of the extension <a class="reference external" href="http://code.google.com/p/smart-snippet/">smart-snippet</a>. Both are <p>YASnippet is a re-write of the extension <a class="reference external" href="http://code.google.com/p/smart-snippet/">smart-snippet</a>. Both are
original creations of <a class="reference external" href="http://pluskid.lifegoo.org">pluskid</a>.</p> original creations of <a class="reference external" href="http://pluskid.lifegoo.org">pluskid</a>.</p>
<div class="section" id="video-demo"> <div class="section" id="video-demo">

View File

@ -9,16 +9,15 @@ Yet Another Snippet extension
.. contents:: .. contents::
YASnippet is a template system for Emacs. It allows you to type a **YASnippet** is a template system for Emacs. It allows you to type a
abbreviation and automatically expand the abbreviation into function abbreviation and automatically expand the abbreviation into function
templates. templates.
Bundled language templates includes: C, C++, C#, Perl, Python, Ruby, Bundled language templates includes: C, C++, C#, Perl, Python, Ruby,
SQL, LaTeX, HTML, CSS and more. SQL, LaTeX, HTML, CSS and more.
YASnippet system is inspired from TextMate's template system. You can The snippet syntax is inspired from TextMate's syntax, you can
use `a tool even `import <snippet-development.html#importing-textmate-snippets>`_
<http://yasnippet.googlecode.com/svn/trunk/textmate-import.rb> to
import most TextMate templates to YASnippet. import most TextMate templates to YASnippet.
YASnippet is a re-write of the extension `smart-snippet`_. Both are YASnippet is a re-write of the extension `smart-snippet`_. Both are

View File

@ -3,7 +3,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" /> <meta name="generator" content="Docutils 0.6: http://docutils.sourceforge.net/" />
<title>Writing snippets</title> <title>Writing snippets</title>
<link rel="stylesheet" href="styles.css" type="text/css" /> <link rel="stylesheet" href="styles.css" type="text/css" />
</head> </head>
@ -60,8 +60,8 @@
<li><a class="reference internal" href="#name-snippet-name" id="id8"><tt class="docutils literal"><span class="pre">#</span> <span class="pre">name:</span></tt> snippet name</a></li> <li><a class="reference internal" href="#name-snippet-name" id="id8"><tt class="docutils literal"><span class="pre">#</span> <span class="pre">name:</span></tt> snippet name</a></li>
<li><a class="reference internal" href="#condition-snippet-condition" id="id9"><tt class="docutils literal"><span class="pre">#</span> <span class="pre">condition:</span></tt> snippet condition</a></li> <li><a class="reference internal" href="#condition-snippet-condition" id="id9"><tt class="docutils literal"><span class="pre">#</span> <span class="pre">condition:</span></tt> snippet condition</a></li>
<li><a class="reference internal" href="#expand-env-expand-environment" id="id10"><tt class="docutils literal"><span class="pre">#</span> <span class="pre">expand-env:</span></tt> expand environment</a></li> <li><a class="reference internal" href="#expand-env-expand-environment" id="id10"><tt class="docutils literal"><span class="pre">#</span> <span class="pre">expand-env:</span></tt> expand environment</a></li>
<li><a class="reference internal" href="#binding-direct-keybinding" id="id11"><tt class="docutils literal"><span class="pre">#</span> <span class="pre">binding:</span></tt>: direct keybinding</a></li> <li><a class="reference internal" href="#binding-direct-keybinding" id="id11"><tt class="docutils literal"><span class="pre">#</span> <span class="pre">binding:</span></tt> direct keybinding</a></li>
<li><a class="reference internal" href="#contributor-snippet-author" id="id12"><tt class="docutils literal"><span class="pre">#</span> <span class="pre">contributor:</span></tt>: snippet author</a></li> <li><a class="reference internal" href="#contributor-snippet-author" id="id12"><tt class="docutils literal"><span class="pre">#</span> <span class="pre">contributor:</span></tt> snippet author</a></li>
</ul> </ul>
</li> </li>
<li><a class="reference internal" href="#template-syntax" id="id13">Template syntax</a><ul> <li><a class="reference internal" href="#template-syntax" id="id13">Template syntax</a><ul>
@ -89,6 +89,7 @@
<li><a class="reference internal" href="#yas-after-exit-snippet-hook-and-yas-before-expand-snippet-hook" id="id33"><tt class="docutils literal"><span class="pre">yas/after-exit-snippet-hook</span></tt> and <tt class="docutils literal"><span class="pre">yas/before-expand-snippet-hook</span></tt></a></li> <li><a class="reference internal" href="#yas-after-exit-snippet-hook-and-yas-before-expand-snippet-hook" id="id33"><tt class="docutils literal"><span class="pre">yas/after-exit-snippet-hook</span></tt> and <tt class="docutils literal"><span class="pre">yas/before-expand-snippet-hook</span></tt></a></li>
</ul> </ul>
</li> </li>
<li><a class="reference internal" href="#importing-textmate-snippets" id="id34">Importing TextMate snippets</a></li>
</ul> </ul>
</div> </div>
<div class="section" id="snippet-development"> <div class="section" id="snippet-development">
@ -233,7 +234,7 @@ your hard work. You can then use:</p>
</pre></div> </pre></div>
</div> </div>
<div class="section" id="binding-direct-keybinding"> <div class="section" id="binding-direct-keybinding">
<h2><a class="toc-backref" href="#id11"><tt class="docutils literal"><span class="pre">#</span> <span class="pre">binding:</span></tt>: direct keybinding</a></h2> <h2><a class="toc-backref" href="#id11"><tt class="docutils literal"><span class="pre">#</span> <span class="pre">binding:</span></tt> direct keybinding</a></h2>
<p>You can use this directive to expand a snippet directly from a normal <p>You can use this directive to expand a snippet directly from a normal
Emacs keybinding. The keybinding will be registered in the Emacs Emacs keybinding. The keybinding will be registered in the Emacs
keymap named after the major mode the snippet is active keymap named after the major mode the snippet is active
@ -264,7 +265,7 @@ active and the function <tt class="docutils literal"><span class="pre">yas/kill-
undefine all the keybindings.</p> undefine all the keybindings.</p>
</div> </div>
<div class="section" id="contributor-snippet-author"> <div class="section" id="contributor-snippet-author">
<h2><a class="toc-backref" href="#id12"><tt class="docutils literal"><span class="pre">#</span> <span class="pre">contributor:</span></tt>: snippet author</a></h2> <h2><a class="toc-backref" href="#id12"><tt class="docutils literal"><span class="pre">#</span> <span class="pre">contributor:</span></tt> snippet author</a></h2>
<p>This is optional and has no effect whatsoever on snippet <p>This is optional and has no effect whatsoever on snippet
functionality, but it looks nice.</p> functionality, but it looks nice.</p>
</div> </div>
@ -536,10 +537,46 @@ the next field.</p>
snippet and after exiting the snippet. If you find any strange but snippet and after exiting the snippet. If you find any strange but
functional use for them, that's probably a design flaw in YASnippet, functional use for them, that's probably a design flaw in YASnippet,
so let us know.</p> so let us know.</p>
</div>
</div>
<div class="section" id="importing-textmate-snippets">
<h1><a class="toc-backref" href="#id34">Importing TextMate snippets</a></h1>
<p>There are a couple of tools that take TextMate's &quot;.tmSnippet&quot; xml
files and create YASnippet definitions:</p>
<blockquote>
<ul class="simple">
<li><a class="reference external" href="http://code.nokrev.com/?p=snippet-copier.git;a=blob_plain;f=snippet_copier.py">a python script by Jeff Wheeler</a></li>
<li>a <a class="reference external" href="http://yasnippet.googlecode.com/svn/trunk/extras/textmate_import.rb">ruby tool</a>
, <tt class="docutils literal"><span class="pre">textmate_import.rb</span></tt> adapted from <a class="reference external" href="http://www.neutronflux.net/2009/07/28/shoulda-snippets-for-emacs/">Rob Christie's</a>,
which I have uploaded to the repository.</li>
</ul>
</blockquote>
<p>In this section, i'll shortly cover the <strong>second</strong> option.</p>
<p>Download the <tt class="docutils literal"><span class="pre">textmate_import.rb</span></tt> tool and the TextMate
bundle you're interested in.</p>
<div class="highlight"><pre>$ curl -O http://yasnippet.googlecode.com/svn/trunk/extras/textmate_import.rb
$ svn export http://svn.textmate.org/trunk/Bundles/HTML.tmbundle/
</pre></div>
<p>Then invoke <tt class="docutils literal"><span class="pre">textmate_import.rb</span></tt> like this:</p>
<div class="highlight"><pre>$ ./textmate_import.rb -d HTML.tmbundle/Snippets/ -o html-mode -g HTML.tmbundle/info.plist
</pre></div>
<p>You should end up with a <tt class="docutils literal"><span class="pre">html-mode</span></tt> subdir containing snippets
exported from textmate.</p>
<div class="highlight"><pre>$ tree html-mode # to view dir contents, if you have &#39;tree&#39; installed
</pre></div>
<p>The <tt class="docutils literal"><span class="pre">-g</span></tt> is optional but helps the tool figure out the grouping.
According to <a class="reference external" href="snippet-organization.html">Organizing Snippets</a>, don't forget to touch
<tt class="docutils literal"><span class="pre">.yas-make-groups</span></tt> and <tt class="docutils literal"><span class="pre">.yas-ignore-filename-triggers</span></tt> inside the
<tt class="docutils literal"><span class="pre">html-mode</span></tt> dir.</p>
<p>Also try <tt class="docutils literal"><span class="pre">textmate_import.rb</span> <span class="pre">--help</span></tt> for a list of options.</p>
<p>Please note that snippet importation is not yet perfect. You'll
probably have some adjustments to some/many snippets. Please
contribute these adjustments to the google group or, better yet, patch
the <tt class="docutils literal"><span class="pre">textmate_import.rb</span></tt> to automatically perform them and submit
that.</p>
<!-- LocalWords: html YASnippet yas sourcecode pluskid init filenames filename --> <!-- LocalWords: html YASnippet yas sourcecode pluskid init filenames filename -->
<!-- LocalWords: env varlist keybinding keymap rinari ifndef upcase endif --> <!-- LocalWords: env varlist keybinding keymap rinari ifndef upcase endif -->
<!-- LocalWords: nondirectory autorelease aValue inline --> <!-- LocalWords: nondirectory autorelease aValue inline -->
</div>
</div> </div>
</div> </div>
</div> </div>

View File

@ -183,7 +183,7 @@ your hard work. You can then use:
| | | | | | | |
+--+-+--+ +--+-+--+
``# binding:``: direct keybinding ``# binding:`` direct keybinding
--------------------------------- ---------------------------------
You can use this directive to expand a snippet directly from a normal You can use this directive to expand a snippet directly from a normal
@ -225,7 +225,7 @@ modes and it is hard to undefine them. In particular, the variable
active and the function ``yas/kill-snippet-keybindings`` will try to active and the function ``yas/kill-snippet-keybindings`` will try to
undefine all the keybindings. undefine all the keybindings.
``# contributor:``: snippet author ``# contributor:`` snippet author
--------------------------------------------------- ---------------------------------------------------
This is optional and has no effect whatsoever on snippet This is optional and has no effect whatsoever on snippet
@ -590,6 +590,58 @@ snippet and after exiting the snippet. If you find any strange but
functional use for them, that's probably a design flaw in YASnippet, functional use for them, that's probably a design flaw in YASnippet,
so let us know. so let us know.
Importing TextMate snippets
===========================
There are a couple of tools that take TextMate's ".tmSnippet" xml
files and create YASnippet definitions:
* `a python script by Jeff Wheeler
<http://code.nokrev.com/?p=snippet-copier.git;a=blob_plain;f=snippet_copier.py>`_
* a `ruby tool
<http://yasnippet.googlecode.com/svn/trunk/extras/textmate_import.rb>`_
, ``textmate_import.rb`` adapted from `Rob Christie's
<http://www.neutronflux.net/2009/07/28/shoulda-snippets-for-emacs/>`_,
which I have uploaded to the repository.
In this section, i'll shortly cover the **second** option.
Download the ``textmate_import.rb`` tool and the TextMate
bundle you're interested in.
.. sourcecode:: text
$ curl -O http://yasnippet.googlecode.com/svn/trunk/extras/textmate_import.rb
$ svn export http://svn.textmate.org/trunk/Bundles/HTML.tmbundle/
Then invoke ``textmate_import.rb`` like this:
.. sourcecode:: text
$ ./textmate_import.rb -d HTML.tmbundle/Snippets/ -o html-mode -g HTML.tmbundle/info.plist
You should end up with a ``html-mode`` subdir containing snippets
exported from textmate.
.. sourcecode:: text
$ tree html-mode # to view dir contents, if you have 'tree' installed
The ``-g`` is optional but helps the tool figure out the grouping.
According to `Organizing Snippets`_, don't forget to touch
``.yas-make-groups`` and ``.yas-ignore-filename-triggers`` inside the
``html-mode`` dir.
Also try ``textmate_import.rb --help`` for a list of options.
Please note that snippet importation is not yet perfect. You'll
probably have some adjustments to some/many snippets. Please
contribute these adjustments to the google group or, better yet, patch
the ``textmate_import.rb`` to automatically perform them and submit
that.
.. LocalWords: html YASnippet yas sourcecode pluskid init filenames filename .. LocalWords: html YASnippet yas sourcecode pluskid init filenames filename
.. LocalWords: env varlist keybinding keymap rinari ifndef upcase endif .. LocalWords: env varlist keybinding keymap rinari ifndef upcase endif
.. LocalWords: nondirectory autorelease aValue inline .. LocalWords: nondirectory autorelease aValue inline

View File

@ -134,19 +134,19 @@ class TmSnippet
def to_yasnippet def to_yasnippet
doc = "# -*- mode: snippet -*-\n" doc = "# -*- mode: snippet -*-\n"
doc << "#key: #{self.tab_trigger}\n" if self.tab_trigger doc << "# key: #{self.tab_trigger}\n" if self.tab_trigger
doc << "#contributor : Translated from TextMate Snippet\n" doc << "# contributor: Translated from TextMate Snippet\n"
doc << "#name : #{self.name}\n" doc << "# name: #{self.name}\n"
doc << "#" unless Choice.choices.convert_bindings doc << "#" unless Choice.choices.convert_bindings
doc << "#binding : \"#{self.key_equivalent}\"\n" if self.key_equivalent doc << "# binding: \"#{self.key_equivalent}\"\n" if self.key_equivalent
doc << "# --\n" doc << "# --\n"
@@known_substitutions.each_pair { |k, v| self.content.gsub!(k,v) } @@known_substitutions.each_pair { |k, v| self.content.gsub!(k,v) }
doc << "#{self.content}" doc << "#{self.content}"
end end
def yasnippet_dir(dir) def yasnippet_file(basedir)
dir = File.join(dir,File.dirname(@file)) basedir = File.join(basedir,group) if group
dir = File.join(dir,group) if group File.join(basedir,@file).gsub(/#{File.extname(@file)}$/,".yasnippet")
end end
end end
@ -164,27 +164,27 @@ if $0 == __FILE__
puts "Will try to convert #{snippet_files.length} snippets...\n" puts "Will try to convert #{snippet_files.length} snippets...\n"
snippet_files.each do |file| snippet_files.each do |file|
puts "Processing \"#{File.join(Choice.choices.snippet_dir,file)}\"\n" begin
snippet = TmSnippet.new(file,info_plist) puts "Processing \"#{File.join(Choice.choices.snippet_dir,file)}\"\n"
if Choice.choices.output_dir snippet = TmSnippet.new(file,info_plist)
begin if Choice.choices.output_dir
dir_to_create = snippet.yasnippet_dir(File.join(original_dir, Choice.choices.output_dir)) file_to_create = snippet.yasnippet_file(File.join(original_dir, Choice.choices.output_dir))
FileUtils.mkdir_p(dir_to_create) FileUtils.mkdir_p(File.dirname(file_to_create))
File.open(File.join(dir_to_create,file_to_create), 'w') do |f| File.open(file_to_create, 'w') do |f|
f.write(snippet.to_yasnippet) f.write(snippet.to_yasnippet)
end end
rescue RuntimeError => e else
$stderr.puts "Oops... #{e.class}:#{e.message}" if Choice.choices.print_pretty
puts "--------------------------------------------"
end
puts snippet.to_yasnippet
if Choice.choices.print_pretty
puts "--------------------------------------------"
end
puts "\n\n"
end end
else rescue Exception => e
if Choice.choices.print_pretty $stderr.puts "Oops... #{e.class}:#{e.message}"
puts "--------------------------------------------"
end
puts snippet.to_yasnippet
if Choice.choices.print_pretty
puts "--------------------------------------------"
end
puts "\n\n"
end end
end end
end end