Skip to content

Commit

Permalink
Add new documentation lookup command (Mavericks)
Browse files Browse the repository at this point in the history
The implementation has changed so that it is now possible to use “back” to go back to previous page, it also auto-links things that look like method symbols.

Presently though it does not auto-link methods listed in the includes, class, and instance method sections. Also, it shows the markdown output from ri pretty much as-is.

It uses the version of ri included with OS X 10.9 (Mavericks) so the command is only made available to people on 10.9.
  • Loading branch information
sorbits committed Nov 3, 2013
1 parent dc20944 commit 09e0d1e
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 5 deletions.
34 changes: 34 additions & 0 deletions Commands/Lookup in Documentation (10.9).tmCommand
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>beforeRunningCommand</key>
<string>nop</string>
<key>command</key>
<string>#!/bin/bash
echo "&lt;meta content='0;URL=file://$("${TM_BUNDLE_SUPPORT}/bin/ri_lookup" "${TM_SELECTED_TEXT:-$TM_CURRENT_WORD}")' http-equiv='Refresh'&gt;"
</string>
<key>hideFromUser</key>
<true/>
<key>input</key>
<string>none</string>
<key>inputFormat</key>
<string>text</string>
<key>name</key>
<string>Documentation for Word / Selection</string>
<key>outputCaret</key>
<string>afterOutput</string>
<key>outputFormat</key>
<string>html</string>
<key>outputLocation</key>
<string>newWindow</string>
<key>scope</key>
<string>source.ruby &amp; attr.os-version.10.9</string>
<key>semanticClass</key>
<string>lookup.define.ruby</string>
<key>uuid</key>
<string>FE882491-07FA-4497-B675-97FCF2FC3BF5</string>
<key>version</key>
<integer>2</integer>
</dict>
</plist>
6 changes: 3 additions & 3 deletions Commands/Lookup in Documentation.plist
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
"${TM_BUNDLE_SUPPORT}/bin/linked_ri.rb"</string> "${TM_BUNDLE_SUPPORT}/bin/linked_ri.rb"</string>
<key>fallbackInput</key> <key>fallbackInput</key>
<string>word</string> <string>word</string>
<key>hideFromUser</key>
<true/>
<key>input</key> <key>input</key>
<string>selection</string> <string>selection</string>
<key>inputFormat</key> <key>inputFormat</key>
<string>text</string> <string>text</string>
<key>keyEquivalent</key>
<string>^h</string>
<key>name</key> <key>name</key>
<string>Documentation for Word / Selection</string> <string>Documentation for Word / Selection</string>
<key>outputCaret</key> <key>outputCaret</key>
Expand All @@ -26,7 +26,7 @@
<key>outputLocation</key> <key>outputLocation</key>
<string>toolTip</string> <string>toolTip</string>
<key>scope</key> <key>scope</key>
<string>source.ruby, source.ruby.rails</string> <string>source.ruby</string>
<key>semanticClass</key> <key>semanticClass</key>
<string>lookup.define.ruby</string> <string>lookup.define.ruby</string>
<key>uuid</key> <key>uuid</key>
Expand Down
16 changes: 16 additions & 0 deletions Proxies/Documentation for Word : Selection.tmProxy
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>content</key>
<string>lookup.define.ruby</string>
<key>keyEquivalent</key>
<string>^h</string>
<key>name</key>
<string>Documentation for Word / Selection</string>
<key>scope</key>
<string>source.ruby</string>
<key>uuid</key>
<string>D5660BB2-C554-4694-9E0F-F20CDB6B9EA0</string>
</dict>
</plist>
9 changes: 9 additions & 0 deletions Support/bin/ri_lookup
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

PHRASE=$1

DST=$(mktemp "${TMPDIR:-/tmp}/tm_ruby_ri_XXXXXX").html
"$TM_BUNDLE_SUPPORT/bin/ri_to_html" &>"$DST" "$PHRASE"
echo -n "$DST"

{ sleep 300; rm "$DST"; rm "${DST%.html}"; } &>/dev/null &
54 changes: 54 additions & 0 deletions Support/bin/ri_to_html
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -wKU
require "#{ENV['TM_SUPPORT_PATH']}/lib/tm/htmloutput.rb"
require "shellwords"

RI_EXE = '/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ri'

def make_link(text, term)
"<a href='javascript:lookup(&quot;#{term.shellescape}&quot;)' title='Lookup ‘#{term}’'>#{text}</a>"
end

phrase = *ARGV
page = %x{ #{RI_EXE} --no-pager -f markdown #{phrase.shellescape} 2>&1 }

page.gsub!(/&/, '&amp;')
page.gsub!(/</, '&lt;')
page.gsub!(/\b([A-Z][A-Za-z]+)(\.|::|#)([\w_]+\b[!?]?)/) do
make_link($1, $1) + $2 + make_link($3, $&)
end

STDOUT << [0xFEFF].pack("U*") # Output UTF-8 ByteOrderMark since this is the only way I have been able to make WebKit show the file as UTF-8. It ignores the <meta> tag and it also ignores the com.apple.TextEncoding extended attribute.

TextMate::HTMLOutput.show(:title => "Documentation for “#{phrase}”", :sub_title => `#{RI_EXE} --version`) do |io|
io << '<script type="text/javascript">function lookup (phrase) { window.location="file://" + TextMate.system(\'"$TM_BUNDLE_SUPPORT/bin/ri_lookup" \' + phrase, null).outputString }</script>' << "\n"

in_pre, whitespace = false, ""
page.each_line do |line|
if line =~ /^(#+) (.+)/
if in_pre
io << "</pre>"
in_pre = false
end
io << "<h#{$1.size}>#$2</h#{$1.size}>\n"
elsif line =~ /^---$/
if in_pre
io << "</pre>"
in_pre = false
end
io << "<hr>\n"
elsif line =~ /^\s*$/
whitespace << line
else
if in_pre
io << whitespace
else
io << "<pre>\n"
in_pre = true
end
io << line
whitespace = ""
end
end

io << "</pre>" if in_pre
end
4 changes: 2 additions & 2 deletions info.plist
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<string>35222962-C50D-4D58-A6AE-71E7AD980BE4</string> <string>35222962-C50D-4D58-A6AE-71E7AD980BE4</string>
<string>5289EE40-86B8-11D9-A8D4-000A95E13C98</string> <string>5289EE40-86B8-11D9-A8D4-000A95E13C98</string>
<string>------------------------------------</string> <string>------------------------------------</string>
<string>63F3B3B7-CBE2-426B-B551-657733F3868B</string> <string>D5660BB2-C554-4694-9E0F-F20CDB6B9EA0</string>
<string>6EA7AE06-3EA9-497D-A6DE-732DE43DA6E9</string> <string>6EA7AE06-3EA9-497D-A6DE-732DE43DA6E9</string>
<string>------------------------------------</string> <string>------------------------------------</string>
<string>8646378E-91F5-4771-AC7C-43FC49A93576</string> <string>8646378E-91F5-4771-AC7C-43FC49A93576</string>
Expand Down Expand Up @@ -436,7 +436,7 @@
<array> <array>
<string>35222962-C50D-4D58-A6AE-71E7AD980BE4</string> <string>35222962-C50D-4D58-A6AE-71E7AD980BE4</string>
<string>5289EE40-86B8-11D9-A8D4-000A95E13C98</string> <string>5289EE40-86B8-11D9-A8D4-000A95E13C98</string>
<string>63F3B3B7-CBE2-426B-B551-657733F3868B</string> <string>D5660BB2-C554-4694-9E0F-F20CDB6B9EA0</string>
<string>1AD6A138-2E89-4D6A-AB3F-416BF9CE968D</string> <string>1AD6A138-2E89-4D6A-AB3F-416BF9CE968D</string>
<string>931DD73E-615E-476E-9B0D-8341023AE730</string> <string>931DD73E-615E-476E-9B0D-8341023AE730</string>
<string>DAA69A0C-FC1E-4509-9931-DFFB38B4D6AE</string> <string>DAA69A0C-FC1E-4509-9931-DFFB38B4D6AE</string>
Expand Down

0 comments on commit 09e0d1e

Please sign in to comment.