Skip to content

Commit a84574e

Browse files
committed
First commit
0 parents  commit a84574e

File tree

13 files changed

+297
-0
lines changed

13 files changed

+297
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Bundler
2+
/.bundle
3+
/vendor/bundle
4+
5+
# OS Specific
6+
.DS_Store
7+
Thumbs.db

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'rake'
4+
gem 'plist'

Gemfile.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
plist (3.1.0)
5+
rake (0.9.6)
6+
7+
PLATFORMS
8+
ruby
9+
10+
DEPENDENCIES
11+
plist
12+
rake

Qiita.alfredworkflow

15.4 KB
Binary file not shown.

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Alfred Qiita Workflow
2+
3+
## Installation
4+
5+
```
6+
$ bundle install --path vendor/bundle --binstubs=.bundle/bin
7+
$ rake link
8+
```
9+
10+
If you using Alfred with Dropbox Sync folder, you should type `rake install` instead of 'rake link'.
11+
12+
## Thanks
13+
14+
- [Alfred 2 Ruby Template](https://github.com/zhaocai/alfred2-ruby-template)

Rakefile

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require 'yaml'
2+
require 'plist'
3+
4+
config_file = 'config.yml'
5+
workflow_home = File.expand_path('~/Library/Application Support/Alfred 2/Alfred.alfredpreferences/workflows')
6+
7+
$config = YAML.load_file config_file
8+
$config['bundleid'] = "#{$config['domain']}.#{$config['id']}"
9+
$config['package_file'] = File.join($config['path'], 'info.plist')
10+
$config['dbx_workflow_path'] = File.join(File.expand_path($config['dropbox']), '/Alfred.alfredpreferences/workflows')
11+
12+
desc "Generate a plist file for Alfred from config.yml"
13+
task :configure do
14+
package = Plist::parse_xml $config['package_file']
15+
16+
unless package['bundleid'].eql?($config["bundleid"])
17+
package['bundleid'] = $config['bundleid']
18+
File.open($config['package_file'], 'wb') do |f|
19+
f.write package.to_plist
20+
end
21+
end
22+
end
23+
24+
desc "Link to Alfred"
25+
task :link => [:configure] do
26+
ln_sf File.expand_path($config["path"]), File.join(workflow_home, $config["bundleid"])
27+
end
28+
29+
desc "Unlink from Alfred"
30+
task :unlink => [:configure] do
31+
rm File.join(workflow_home, $config["bundleid"])
32+
end
33+
34+
desc "Install to Alfred Sync folder on Dropbox"
35+
task :install => [:configure] do
36+
ln_sf File.expand_path($config["path"]), File.join($config["dbx_workflow_path"], $config["bundleid"])
37+
end
38+
39+
desc "Unlink from Alfred Sync folder on Dropbox"
40+
task :uninstall => [:configure] do
41+
rm File.join($config["dbx_workflow_path"], $config["bundleid"])
42+
end

config.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# bundle_id = "domain.id"
2+
# path is the relative path to the workflow in the project root
3+
---
4+
path: workflow
5+
domain: co.randompaper
6+
id: qiita.alfred
7+
# If you are using Alfred's advanced Dropbox sync, indicate the path shown in
8+
# Alfred Preferences > Advanced > Syncing:
9+
dropbox: ~/Dropbox/アプリ

screenshots/qiita-workflow.png

443 KB
Loading

workflow/alfred.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# -*- coding: utf-8 -*-
2+
import itertools
3+
import os
4+
import plistlib
5+
import unicodedata
6+
import sys
7+
8+
from xml.etree.ElementTree import Element, SubElement, tostring
9+
10+
"""
11+
You should run your script via /bin/bash with all escape options ticked.
12+
The command line should be
13+
14+
python yourscript.py "{query}" arg2 arg3 ...
15+
"""
16+
UNESCAPE_CHARACTERS = u""" ;()"""
17+
18+
_MAX_RESULTS_DEFAULT = 9
19+
20+
preferences = plistlib.readPlist('info.plist')
21+
bundleid = preferences['bundleid']
22+
23+
class Item(object):
24+
@classmethod
25+
def unicode(cls, value):
26+
try:
27+
items = value.iteritems()
28+
except AttributeError:
29+
return unicode(value)
30+
else:
31+
return dict(map(unicode, item) for item in items)
32+
33+
def __init__(self, attributes, title, subtitle, icon=None):
34+
self.attributes = attributes
35+
self.title = title
36+
self.subtitle = subtitle
37+
self.icon = icon
38+
39+
def __str__(self):
40+
return tostring(self.xml(), encoding='utf-8')
41+
42+
def xml(self):
43+
item = Element(u'item', self.unicode(self.attributes))
44+
for attribute in (u'title', u'subtitle', u'icon'):
45+
value = getattr(self, attribute)
46+
if value is None:
47+
continue
48+
try:
49+
(value, attributes) = value
50+
except:
51+
attributes = {}
52+
SubElement(item, attribute, self.unicode(attributes)).text = unicode(value)
53+
return item
54+
55+
def args(characters=None):
56+
return tuple(unescape(decode(arg), characters) for arg in sys.argv[1:])
57+
58+
def config():
59+
return _create('config')
60+
61+
def decode(s):
62+
return unicodedata.normalize('NFC', s.decode('utf-8'))
63+
64+
def uid(uid):
65+
return u'-'.join(map(unicode, (bundleid, uid)))
66+
67+
def unescape(query, characters=None):
68+
for character in (UNESCAPE_CHARACTERS if (characters is None) else characters):
69+
query = query.replace('\\%s' % character, character)
70+
return query
71+
72+
def work(volatile):
73+
path = {
74+
True: '~/Library/Caches/com.runningwithcrayons.Alfred-2/Workflow Data',
75+
False: '~/Library/Application Support/Alfred 2/Workflow Data'
76+
}[bool(volatile)]
77+
return _create(os.path.join(os.path.expanduser(path), bundleid))
78+
79+
def write(text):
80+
sys.stdout.write(text)
81+
82+
def xml(items, maxresults=_MAX_RESULTS_DEFAULT):
83+
root = Element('items')
84+
for item in itertools.islice(items, maxresults):
85+
root.append(item.xml())
86+
return tostring(root, encoding='utf-8')
87+
88+
def _create(path):
89+
if not os.path.isdir(path):
90+
os.mkdir(path)
91+
if not os.access(path, os.W_OK):
92+
raise IOError('No write access: %s' % path)
93+
return path

workflow/alfred.pyc

3.93 KB
Binary file not shown.

0 commit comments

Comments
 (0)