Changeset 34667

Show
Ignore:
Timestamp:
07/29/09 02:53:46 (4 years ago)
Author:
snaka
Message:

refactoring

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/javascript/vimperator-plugins/trunk/hateDAopener.js

    r34653 r34667  
    8686// }}} 
    8787plugins.hateDAopener = (function(){ 
    88     // UTILITY //////////////////////////////////////////////////////////////{{{ 
    89     let log  = liberator.log; 
    90     let dump = function(title, obj) liberator.dump(title + "\n" + util.objectToString(obj)); 
    91     // \r\n separated string to array 
    92     let rns  = function(source) source.split(/\r?\n/); 
    93     // comma separated string to array 
    94     let csv  = function(source) source.split(","); 
     88 
    9589    let libly = liberator.plugins.libly; 
    96     // }}} 
    9790    // PUBLIC ///////////////////////////////////////////////////////////////{{{ 
    9891    let self = { 
    9992        extractTitleAndTags: extractTitleAndTags, 
    10093        generateCandidates:  generateCandidates, 
    101         getDiaryEntries:    getDiaryEntries, 
    102         getFaviconURI:      getFaviconURI, 
     94        getDiaryEntries:     getDiaryEntries, 
     95        getFaviconURI:       getFaviconURI, 
    10396    }; 
    10497    // }}} 
     
    119112              context.filterFunc = null; 
    120113              context.regenerate = true; 
    121               context.generate = function() inputFilter(args); 
     114              context.generate = function() filteredCandidates(args); 
    122115            }, 
    123116            argCount: "*", 
     
    129122    // }}} 
    130123    // PRIVATE //////////////////////////////////////////////////////////////{{{ 
    131     let cache = { }; 
    132124 
    133125    /** 
     
    138130     *           ['snaka72', 'vimperator.g']] 
    139131     */ 
    140     function accounts() { 
    141         return liberator.globalVariables.hateDAopener_accounts || []; 
    142     } 
     132    function accounts() 
     133        liberator.globalVariables.hateDAopener_accounts || []; 
    143134 
    144135    /** 
    145136     * filter candidates by words 
    146137     */ 
    147     function inputFilter(words) { 
    148         var start = new Date; 
    149         dump(words.join(',') + "> start:" + start + "\n"); 
    150         words = words.map(function(i) i.toLowerCase()); 
    151         let filtered = generateCandidates().filter( 
    152             function(i) { 
    153                 let targetString =  [i.tags, i.name].join(",").toLowerCase(); 
    154                 return words.every(function(word) targetString.indexOf(word) > -1); 
    155             } 
    156         ); 
    157         var end = new Date; 
    158         dump(words.join(',') + "> end  :" + end + "[" + (end - start) + "]\n"); 
    159         return filtered; 
    160     } 
     138    function filteredCandidates(words) { 
     139        return  generateCandidates() 
     140                .filter(function(i) 
     141                    let (targetString = '' + i.tags + ' ' + i.name) 
     142                    words.every(function(word) targetString.match(word, 'i')) 
     143                ); 
     144    } 
     145 
     146    function hatenaDiaryUrl(diary, userId) 
     147        'http://' + diary + '.hatena.ne.jp/' + userId; 
    161148 
    162149    /** 
     
    165152     */ 
    166153    function generateCandidates() { 
    167       dump("generateCandidates"); 
    168154      let allEntries = []; 
     155      let notEmpty = function(i) i && i != ""; 
     156 
    169157      accounts().forEach(function([userId, diary]) { 
    170           let entries =  getDiaryEntries(userId, diary); 
    171           entries = entries.filter(function(i) i && i != ""); 
    172           entries =  entries.map(function([dateTime, path, titleAndTag]) { 
    173               let url = 'http://' + diary + '.hatena.ne.jp/' + userId + path; 
    174               let title, tags; 
    175               [title, tags] = extractTitleAndTags(titleAndTag); 
    176               return { 
    177                   "url"  : url, 
    178                   "baseUrl" : 'http://' + diary + '.hatena.ne.jp/' + userId, 
    179                   "path" : path, 
    180                   "name" : title, 
    181                   "tags" : tags 
    182               }; 
    183           }); 
     158          let entries =  getDiaryEntries(userId, diary) 
     159          .filter(notEmpty) 
     160          .map(function([dateTime, path, titleAndTag]) 
     161              let ([title, tags] = extractTitleAndTags(titleAndTag)) { 
     162                  "url"     : hatenaDiaryUrl(diary, userId) + path, 
     163                  "baseUrl" : hatenaDiaryUrl(diary, userId), 
     164                  "path"    : path, 
     165                  "name"    : title, 
     166                  "tags"    : tags 
     167              } 
     168          ); 
    184169          allEntries = allEntries.concat(entries); 
    185170      }); 
     
    192177     * @return [String dateTime, String path, String titleAndTag] 
    193178     */ 
    194     function getDiaryEntries(userId, diary) { 
    195         dump("getDalyEntries"); 
    196         if (cache[diary + userId]) 
    197             return cache[diary + userId]; 
    198  
    199         let req = new libly.Request( 
    200             "http://"+ diary + ".hatena.ne.jp/" + userId + "/archive/plaintext", 
    201             null, 
    202             { asynchronous: false } 
    203         ); 
    204         let result; 
    205         req.addEventListener("onSuccess", function(data) { 
    206             dump("request succeeded", data); 
    207             result = data; 
    208         }); 
    209         req.addEventListener("onFailure", function(data) { 
    210             dump("request failed", data); 
    211             return; 
    212         }); 
    213         req.get(); 
    214  
    215         let entries = rns(result.responseText); 
    216         entries = entries.map(function(i) i.split(',')); 
    217         return cache[diary + userId] = entries; 
    218     } 
     179    let getDiaryEntries = (function() { 
     180        let cache = {}; 
     181        return function(userId, diary) { 
     182            if (cache[diary + userId]) 
     183                return cache[diary + userId]; 
     184            let res = util.httpGet(hatenaDiaryUrl(diary, userId) + "/archive/plaintext"); 
     185            return cache[diary + userId] = res.responseText 
     186                                           .split(/\r?\n/) 
     187                                           .map(function(i) i.split(',')); 
     188        }; 
     189    })(); 
    219190 
    220191    /** 
     
    223194     */ 
    224195    function extractTitleAndTags(titleAndTag) { 
    225         let patternTagAndTitle = /(\[.*\])?(.*)/; 
    226         if (!patternTagAndTitle.test(titleAndTag)) 
    227             return []; 
    228  
    229         let tags, title; 
    230         [,tags, title] = titleAndTag.match(patternTagAndTitle); 
    231  
    232         let patternTags = /\[([^\]]+)\]/g; 
    233         if (!patternTags.test(tags)) 
    234             return [title, []]; 
    235  
    236         tags = tags.match(patternTags); 
     196        let patternTags = /\[[^\]]+\]/g; 
    237197        return [ 
    238             title, 
    239             tags 
     198            titleAndTag.replace(patternTags, ''), 
     199            (titleAndTag.match(patternTags) || []) 
    240200        ]; 
    241     } 
    242  
    243     function templateTags(item){ 
    244       return item.tags && item.tags.length > 0 ? item.tags.join("")  : ""; 
    245201    } 
    246202 
     
    257213    } 
    258214 
    259     let faviconCache = {}; 
    260     function getFaviconURI(pageURI) { 
    261         if (faviconCache[pageURI]) 
    262             return faviconCache[pageURI]; 
    263         try { 
     215    function templateTags(item){ 
     216      return item.tags && item.tags.length > 0 ? item.tags.join("")  : ""; 
     217    } 
     218 
     219    let getFaviconURI = (function() { 
     220        let faviconCache = {}; 
     221 
     222        return function (pageURI) { 
     223            if (faviconCache[pageURI]) 
     224                return faviconCache[pageURI]; 
     225 
    264226            let uri = Cc["@mozilla.org/network/io-service;1"] 
    265227                    .getService(Ci.nsIIOService) 
     
    269231                    .getFaviconImageForPage(uri); 
    270232            return faviconCache[pageURI] = faviconURI.spec; 
    271         } catch(e) { 
    272             alert(pageURI); 
    273             return ""; 
    274233        } 
    275     } 
    276  
    277     // }}} 
    278  
     234    })(); 
     235 
     236    // }}} 
     237    // UTILITY //////////////////////////////////////////////////////////////{{{ 
     238    function dump(title, obj) 
     239        liberator.dump(title + "\n" + util.objectToString(obj)); 
     240 
     241    function rns(source) 
     242        source.split(/\r?\n/); 
     243 
     244    function csv(source) 
     245        source.split(","); 
     246 
     247    // }}} 
    279248    return self; 
    280249})();