LoginSignup
11

More than 5 years have passed since last update.

"Qiita API for VIM"

Last updated at Posted at 2012-10-10

mattnさんqiita-vimをお使い下さい
http://qiita.com/items/4b31a7ecd0a1fd761fe1

qiita.vim
scriptencoding utf-8

let s:save_cpo = &cpo
set cpo&vim

command! -nargs=1 QiitaPost call s:QiitaPost(<f-args>, 0)

if !exists('g:qiita_user')
    let g:qiita_user = ''
endif

let s:qiita_base_url = 'https://qiita.com/api/v1/'

function! s:QiitaAuth()
    if !strlen(g:qiita_user)
        let qiita_user = input('Qiita user :')
    else
        let qiita_user = g:qiita_user
    endif
    return s:QiitaAuthWithUser(qiita_user)
endfunction

function! s:QiitaAuthWithUser(user)
    let g:qiita_user = a:user

    let password = inputsecret("Qiita Password for ".g:qiita_user.":")
    if !len(password)
        echo 'Canceled'
        return []
    endif

    redraw | echon 'Posting it to qiita... '
    let res = webapi#http#post(s:qiita_base_url . 'auth' . '?url_name=' . g:qiita_user . '&password=' . password)

    let status = matchstr(matchstr(res.header, '^Status:'), '^[^:]\+: \zs.*')
    if status =~ '^2'
    let obj = webapi#json#decode(res.content)
        return obj["token"]
    else
        echoerr 'Auth failed'
    endif
endfunction

function! s:QiitaPost(title, private)
    if !exists('g:qiita_token')
        let g:qiita_token = s:QiitaAuth()
        if !len(g:qiita_token)
            return
        endif
    endif

    " Get current buffer
    let content = join(getline(1, line('$')), "\n")

    " Get current filename
    let filename = expand('%:t')
    if filename == ''
    let filename = 'hoge'
    end

    let ext = '.' . fnamemodify(filename, ':e')
    let tags = []
    if has_key(s:extmap, ext)
      let type = s:extmap[ext]
    else
      let type = "hoge"
    end
    call add(tags, {"name": type})

    let obj = { "title": a:title, "body": s:MarkDownWrap(filename, type, content), "tags": tags, "private": function('webapi#json#false')}
    if a:private | let obj["private"] = function('webapi#json#true') | endif

    let header = {"Content-Type": "application/json"}
    let res = webapi#http#post(s:qiita_base_url . 'items'. '?token=' . g:qiita_token, webapi#json#encode(obj), header)

    let status = matchstr(matchstr(res.header, '^Status:'), '^[^:]\+: \zs.*')
    if status =~ '^2'
    let obj = webapi#json#decode(res.content)
        echo 'Done: uuid=' . obj["uuid"]
    return obj["uuid"]
    else
        echoerr 'Post failed'
    endif
endfunction

function! s:MarkDownWrap(filename, type, content)
  return "\n``` " . a:type . ":" . a:filename . "\n" . a:content . "\n```\n"
endfunction

let s:extmap = {
      \".adb": "ada",
      \".ahk": "ahk",
      \".arc": "arc",
      \".as": "actionscript",
      \".asm": "asm",
      \".asp": "asp",
      \".aw": "php",
      \".b": "b",
      \".bat": "bat",
      \".befunge": "befunge",
      \".bmx": "bmx",
      \".boo": "boo",
      \".c-objdump": "c-objdump",
      \".c": "c",
      \".cfg": "cfg",
      \".cfm": "cfm",
      \".ck": "ck",
      \".cl": "cl",
      \".clj": "clj",
      \".cmake": "cmake",
      \".coffee": "coffee",
      \".cpp": "cpp",
      \".cppobjdump": "cppobjdump",
      \".cs": "csharp",
      \".css": "css",
      \".cw": "cw",
      \".d-objdump": "d-objdump",
      \".d": "d",
      \".darcspatch": "darcspatch",
      \".diff": "diff",
      \".duby": "duby",
      \".dylan": "dylan",
      \".e": "e",
      \".ebuild": "ebuild",
      \".eclass": "eclass",
      \".el": "lisp",
      \".erb": "erb",
      \".erl": "erlang",
      \".f90": "f90",
      \".factor": "factor",
      \".feature": "feature",
      \".fs": "fs",
      \".fy": "fy",
      \".go": "go",
      \".groovy": "groovy",
      \".gs": "gs",
      \".gsp": "gsp",
      \".haml": "haml",
      \".hs": "haskell",
      \".html": "html",
      \".hx": "hx",
      \".ik": "ik",
      \".ino": "ino",
      \".io": "io",
      \".j": "j",
      \".java": "java",
      \".js": "javascript",
      \".json": "json",
      \".jsp": "jsp",
      \".kid": "kid",
      \".lhs": "lhs",
      \".lisp": "lisp",
      \".ll": "ll",
      \".lua": "lua",
      \".ly": "ly",
      \".m": "objc",
      \".mak": "mak",
      \".man": "man",
      \".mao": "mao",
      \".matlab": "matlab",
      \".md": "markdown",
      \".minid": "minid",
      \".ml": "ml",
      \".moo": "moo",
      \".mu": "mu",
      \".mustache": "mustache",
      \".mxt": "mxt",
      \".myt": "myt",
      \".n": "n",
      \".nim": "nim",
      \".nu": "nu",
      \".numpy": "numpy",
      \".objdump": "objdump",
      \".ooc": "ooc",
      \".parrot": "parrot",
      \".pas": "pas",
      \".pasm": "pasm",
      \".pd": "pd",
      \".phtml": "phtml",
      \".pir": "pir",
      \".pl": "perl",
      \".po": "po",
      \".py": "python",
      \".pytb": "pytb",
      \".pyx": "pyx",
      \".r": "r",
      \".raw": "raw",
      \".rb": "ruby",
      \".rhtml": "rhtml",
      \".rkt": "rkt",
      \".rs": "rs",
      \".rst": "rst",
      \".s": "s",
      \".sass": "sass",
      \".sc": "sc",
      \".scala": "scala",
      \".scm": "scheme",
      \".scpt": "scpt",
      \".scss": "scss",
      \".self": "self",
      \".sh": "sh",
      \".sml": "sml",
      \".sql": "sql",
      \".st": "smalltalk",
      \".tcl": "tcl",
      \".tcsh": "tcsh",
      \".tex": "tex",
      \".textile": "textile",
      \".tpl": "smarty",
      \".twig": "twig",
      \".txt" : "text",
      \".v": "verilog",
      \".vala": "vala",
      \".vb": "vbnet",
      \".vhd": "vhdl",
      \".vim": "vim",
      \".weechatlog": "weechatlog",
      \".xml": "xml",
      \".xq": "xquery",
      \".xs": "xs",
      \".yml": "yaml",
      \}

let &cpo = s:save_cpo
unlet s:save_cpo

下記を参考に作りました。
https://github.com/mattn/gist-vim
https://github.com/motemen/hatena-vim

webapi-vimが必要です。
https://github.com/mattn/webapi-vim
commandも記述してしまってるのでpluginにコピペすれば動くと思います。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11