diff --git a/zencoding.vim b/zencoding.vim index 83b71b9..bb33f31 100644 --- a/zencoding.vim +++ b/zencoding.vim @@ -94,8 +94,10 @@ for item in [ \ {'mode': 'n', 'var': 'user_zen_splitjointag_key', 'key': 'j', 'plug': 'ZenCodingSplitJoinTagNormal', 'func': ':call zen_splitJoinTag()'}, \ {'mode': 'i', 'var': 'user_zen_removetag_key', 'key': 'k', 'plug': 'ZenCodingRemoveTag', 'func': ':call zen_removeTag()a'}, \ {'mode': 'n', 'var': 'user_zen_removetag_key', 'key': 'k', 'plug': 'ZenCodingRemoveTag', 'func': ':call zen_removeTag()'}, -\ {'mode': 'i', 'var': 'user_zen_anchorizeurl_key', 'key': 'a', 'plug': 'ZenCodingAnchorizeURL', 'func': ':call zen_anchorizeURL()a'}, -\ {'mode': 'n', 'var': 'user_zen_anchorizeurl_key', 'key': 'a', 'plug': 'ZenCodingAnchorizeURL', 'func': ':call zen_anchorizeURL()'}, +\ {'mode': 'i', 'var': 'user_zen_anchorizeurl_key', 'key': 'a', 'plug': 'ZenCodingAnchorizeURL', 'func': ':call zen_anchorizeURL(0)a'}, +\ {'mode': 'n', 'var': 'user_zen_anchorizeurl_key', 'key': 'a', 'plug': 'ZenCodingAnchorizeURL', 'func': ':call zen_anchorizeURL(0)'}, +\ {'mode': 'i', 'var': 'user_zen_anchorizesummary_key', 'key': 'A', 'plug': 'ZenCodingAnchorizeSummary', 'func': ':call zen_anchorizeURL(1)a'}, +\ {'mode': 'n', 'var': 'user_zen_anchorizesummary_key', 'key': 'A', 'plug': 'ZenCodingAnchorizeSummary', 'func': ':call zen_anchorizeURL(1)'}, \] if !hasmapto(''.item.plug, item.mode) @@ -1062,7 +1064,11 @@ function! s:zen_toString(...) let child_inline = 0 endif for child in current.child - let inner .= s:zen_toString(child, type, child_inline, filter) + let html = s:zen_toString(child, type, child_inline, filter) + if child.name == 'br' + let inner = substitute(inner, '\n\s*$', '', '') + endif + let inner .= html endfor if len(current.child) if inline == 0 @@ -1504,7 +1510,54 @@ function! s:zen_balanceTag(flag) endif endfunction -function! s:zen_anchorizeURL() +function! s:getContentFromURL(url) + silent! new + silent! exec '0r!curl -s -L "'.substitute(a:url, '#.*', '', '').'"' + if executable('nkf') + if &enc == 'utf-8' + silent! %!nkf -X8 + elseif &enc == 'cp932' + silent! %!nkf -Xs + endif + endif + let ret = join(getline(1, '$'), "\n") + silent! bw! + return ret +endfunction + +function! s:getTextFromHTML(buf) + let threshold_len = 100 + let threshold_per = 0.1 + let buf = a:buf + + let buf = strpart(buf, stridx(buf, '')) + let res = '' + let max = 0 + let mx = '\(]\{-}>\)\|\(<\/td>\)\|\(]\{-}>\)\|\(<\/div>\)' + let m = split(buf, mx) + for str in m + let c = split(str, '<[^>]*?>') + let str = substitute(str, '<[^>]\{-}>', '', 'g') + let str = substitute(str, '>', '>', 'g') + let str = substitute(str, '<', '<', 'g') + let str = substitute(str, '"', '"', 'g') + let str = substitute(str, ''', "'", 'g') + let str = substitute(str, ' ', ' ', 'g') + let str = substitute(str, '¥', '\¥', 'g') + let str = substitute(str, '&', '\&', 'g') + let l = len(str) + if l > threshold_len + let per = len(c) / l + if max < l && per < threshold_per + let max = l + let res = str + endif + endif + endfor + return res +endfunction + +function! s:zen_anchorizeURL(flag) let pos = getpos('.') let mx = 'https\=:\/\/[-!#$%&*+,./:;=?@0-9a-zA-Z_~]\+' let pos1 = searchpos(mx, 'bcnW') @@ -1514,22 +1567,39 @@ function! s:zen_anchorizeURL() return endif - silent! new - silent! exec '0r!curl -s -L "'.substitute(url, '#.*', '', '').'"' - if executable('nkf') - if &enc == 'utf-8' - silent! %!nkf -X8 - elseif &enc == 'cp932' - silent! %!nkf -Xs - endif + let content = s:getContentFromURL(url) + let content = substitute(content, '\n', '', 'g') + let content = substitute(content, '\n\s*\n', '\n', 'g') + let head = strpart(content, 0, stridx(content, '')) + let title = substitute(head, '.*]*>\([^<]\+\)<\/title[^>]*>.*', '\1', 'g') + + if a:flag == 0 + let a = s:zen_parseTag('') + let a.attr['href'] = url + let a.value = title + let expand = s:zen_toString(a, 'html', 0, '') + else + let body = strpart(content, stridx(content, '')) + let body = s:getTextFromHTML(body) + let body = '{' . substitute(body, '^\(.\{0,100}\).*', '\1', '') . '...}' + + let blockquote = s:zen_parseTag('
') + let a = s:zen_parseTag('') + let a.attr['href'] = url + let a.value = '{' . title . '}' + call add(blockquote.child, a) + call add(blockquote.child, s:zen_parseTag('
')) + let p = s:zen_parseTag('

') + let p.value = body + call add(blockquote.child, p) + let cite = s:zen_parseTag('') + let cite.value = '{' . url . '}' + call add(blockquote.child, cite) + let expand = s:zen_toString(blockquote, 'html', 0, '') + let expand = substitute(expand, '|', '', 'g') + let indent = substitute(getline('.'), '^\(\s*\).*', '\1', '') + let expand = substitute(expand, "\n", "\n" . indent, 'g') endif - silent! %join! - silent! %g/^\s*$/d _ - silent! %s/^.\{-}]*>\([^<]\+\)<\/title[^>]*>.*/\1/i - let ret = getline('.') - silent! bw! - let current = s:zen_parseIntoTree(printf('a[href=%s]{%s}', url, ret), '').child[0] - let expand = s:zen_toString(current, 'html', 0, '') call s:change_content(block, expand) endfunction