diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/README.md b/README.md new file mode 100644 index 0000000..ca3bc22 --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# hain-plugin-meme + +[![npm](https://img.shields.io/npm/dt/hain-plugin-meme.svg)](https://www.npmjs.com/package/hain-plugin-meme +) [![npm](https://img.shields.io/npm/dm/hain-plugin-meme.svg)](https://www.npmjs.com/package/hain-plugin-meme +) [![GitHub release](https://img.shields.io/github/release/metrakit/hain-plugin-meme.svg)](https://www.npmjs.com/package/hain-plugin-meme) + +A plugin for generate fastly a meme in the [Hain](https://github.com/appetizermonster/hain) app (Alfred clone for Windows). +This plugin use the [Memegen API](http://memegen.link). + +## Install + +Type this command in the Hain input : +``` +/hpm install meme +``` + +## Usage + +``` +/meme I'm awesome / Yeah ! / +``` + +### Credit + +Thanks to [Memegen](http://memegen.link/) diff --git a/index.js b/index.js new file mode 100644 index 0000000..4718305 --- /dev/null +++ b/index.js @@ -0,0 +1,70 @@ +'use strict'; + +const got = require('got'); +const _ = require('lodash'); +const fs = require('fs'); +const path = require('path'); +const ncp = require("copy-paste"); + +const tplUrl = 'http://memegen.link/templates/'; + +module.exports = (context) => { + const shell = context.shell; + const logger = context.logger; + + let html = []; + let tpls = []; + + function startup() { + html = fs.readFileSync(path.join(__dirname, 'preview.html'), 'utf8'); + got(tplUrl).then(response => { + tpls = JSON.parse(response.body); + }); + } + + function search(query, res) { + const query_trim = query.trim(); + const query_parts = _.split(query_trim, '/', 3); + + logger.log(query_parts); + logger.log(query_parts.length); + + res.add({ + id: `help`, + payload: 'help', + title: 'How to', + desc: 'For generate a meme type : /meme Example / awesome meme /' + }); + + if (query_parts.length === 3) { + return _.forEach(tpls, (url, name) => { + url = _.replace(url, 'templates/', ''); + url += '/' + encodeURIComponent(query_parts[0]) + '/' + encodeURIComponent(query_parts[1]) + '.jpg'; + res.add({ + id: url, + payload: 'open', + title: name, + desc: 'Click for copy the meme to your clipboard', + icon: '#fa fa-file-image-o', + preview: true + }); + }); + } + } + + function execute(url, payload) { + if (payload !== 'open') + return; + ncp.copy(url, function() { + context.toast.enqueue('Pasted to clipboard !'); + }) + } + + function renderPreview(url, payload, render) { + var preview = html.replace('%picture%', url); + preview = preview.replace('%url%', url); + render(preview); + } + + return { startup, search, execute, renderPreview }; +}; diff --git a/logo.png b/logo.png new file mode 100644 index 0000000..1638b7d Binary files /dev/null and b/logo.png differ diff --git a/package.json b/package.json new file mode 100644 index 0000000..9ecda0a --- /dev/null +++ b/package.json @@ -0,0 +1,31 @@ +{ + "name": "hain-plugin-meme", + "version": "0.0.1", + "description": "hain plugin to search a meme on giphy.com", + "main": "index.js", + "scripts": {}, + "repository": { + "type": "git", + "url": "git+https://github.com/metrakit/hain-plugin-meme.git" + }, + "author": "Jordane JOUFFROY", + "license": "MIT", + "keywords": [ + "hain0" + ], + "bugs": { + "url": "https://github.com/metrakit/hain-plugin-meme/issues" + }, + "homepage": "https://github.com/metrakit/hain-plugin-meme", + "dependencies": { + "got": "^6.2.0", + "lodash": "^4.6.1", + "copy-paste": "^1.1.4" + }, + "hain": { + "prefix": "/meme", + "usage": "/meme an awesome meme", + "icon": "/logo.png", + "redirect": "/meme " + } +} diff --git a/preview.html b/preview.html new file mode 100644 index 0000000..b5cf6f1 --- /dev/null +++ b/preview.html @@ -0,0 +1,26 @@ + + + + +
+ +
+ + + \ No newline at end of file