|
| 1 | +# Keypirinha launcher (keypirinha.com) |
| 2 | + |
| 3 | +import keypirinha as kp |
| 4 | +import keypirinha_util as kpu |
| 5 | +import keypirinha_net as kpnet |
| 6 | +from .ClassSuggestion import ClassSuggestion |
| 7 | +from .FunctionSuggestion import FunctionSuggestion |
| 8 | +from .Suggestion import Suggestion |
| 9 | +import json |
| 10 | +from datetime import datetime |
| 11 | + |
| 12 | +import os, sys |
| 13 | +sys.path.append(os.path.join(os.path.dirname(__file__), "lib")) |
| 14 | + |
| 15 | +from bs4 import BeautifulSoup |
| 16 | + |
| 17 | + |
| 18 | +class PHPDocSearch(kp.Plugin): |
| 19 | + """ |
| 20 | + Search and open php doc pages. |
| 21 | + """ |
| 22 | + # The php documentation website |
| 23 | + PHPDOC_WEBSITE = 'http://php.net' |
| 24 | + |
| 25 | + # The path to the URL used to get the documentation index |
| 26 | + PHPDOC_SEARCHINDEX_URL = '/js/search-index.php' |
| 27 | + |
| 28 | + # The name of the file inside cache folder used to save the search index from php.net website |
| 29 | + INDEX_FILENAME = 'searchindex.json' |
| 30 | + |
| 31 | + # Number of days the documentation index should be updated |
| 32 | + DAYS_KEEP_CACHE = 7 |
| 33 | + |
| 34 | + def __init__(self): |
| 35 | + super().__init__() |
| 36 | + self.doc_index = [] |
| 37 | + |
| 38 | + def on_start(self): |
| 39 | + self.generate_search_index() |
| 40 | + self.get_documentation_index() |
| 41 | + self.create_actions() |
| 42 | + pass |
| 43 | + |
| 44 | + def on_catalog(self): |
| 45 | + self.set_catalog([ |
| 46 | + self.create_item( |
| 47 | + category=kp.ItemCategory.KEYWORD, |
| 48 | + label="PHP Documentation", |
| 49 | + short_desc="Search for functions, classes and more in the official PHP documentation", |
| 50 | + target="phpdoc", |
| 51 | + args_hint=kp.ItemArgsHint.REQUIRED, |
| 52 | + hit_hint=kp.ItemHitHint.KEEPALL |
| 53 | + ) |
| 54 | + ]) |
| 55 | + |
| 56 | + def on_suggest(self, user_input, items_chain): |
| 57 | + if not items_chain or items_chain[0].category() != kp.ItemCategory.KEYWORD: |
| 58 | + return |
| 59 | + |
| 60 | + if len(user_input) > 0 and self.should_terminate(0.250): |
| 61 | + return |
| 62 | + |
| 63 | + mylist = self.filterbyvalue(self.doc_index, user_input) |
| 64 | + if (len(mylist)): |
| 65 | + self.set_suggestions(mylist, kp.Match.ANY, kp.Sort.LABEL_ASC) |
| 66 | + |
| 67 | + def filterbyvalue(self, seq, value): |
| 68 | + return list(filter(lambda item: value.lower() in item.label().lower(), seq)) |
| 69 | + |
| 70 | + def on_execute(self, item, action): |
| 71 | + data_bag = item.data_bag().split('|') |
| 72 | + url_to_open = "http://php.net/manual/en/{}.php".format(data_bag[0]) |
| 73 | + |
| 74 | + # Open in the browser |
| 75 | + if ((action and action.name() == 'open_url') or not action): |
| 76 | + kpu.web_browser_command(private_mode=None, new_window=None, url=url_to_open, execute=True) |
| 77 | + return |
| 78 | + |
| 79 | + # Copy its signature |
| 80 | + if (action and (action.name() in ['copy_signature'])): |
| 81 | + opener = kpnet.build_urllib_opener() |
| 82 | + with opener.open(url_to_open) as request: |
| 83 | + response = request.read() |
| 84 | + |
| 85 | + soup = BeautifulSoup(response, 'html.parser') |
| 86 | + |
| 87 | + if (action.name() == 'copy_signature'): |
| 88 | + param_elements = soup.findAll("div", {"class": "methodsynopsis"}) |
| 89 | + params = ', '.join(param.get_text() for param in param_elements) |
| 90 | + params = params.replace('\n', '') |
| 91 | + kpu.set_clipboard(params) |
| 92 | + self.log("Signature copied!") |
| 93 | + return |
| 94 | + |
| 95 | + # Create the default actions to the suggestions |
| 96 | + def create_actions(self): |
| 97 | + general_actions = [ |
| 98 | + self.create_action(name="open_url", label="Open in php.net", short_desc="Open the documentation in php.net") |
| 99 | + ] |
| 100 | + |
| 101 | + function_actions = [ |
| 102 | + self.create_action(name="copy_signature", label="Copy signature", short_desc="Copy the function signature") |
| 103 | + ] |
| 104 | + |
| 105 | + self.set_actions(FunctionSuggestion.ITEMCAT, function_actions + general_actions) |
| 106 | + self.set_actions(ClassSuggestion.ITEMCAT, general_actions) |
| 107 | + |
| 108 | + # Download the search index from php.net website and write it to plugin's cache folder |
| 109 | + def generate_search_index(self): |
| 110 | + search_index_filepath = self.get_search_index_path() |
| 111 | + |
| 112 | + should_generate = False |
| 113 | + try: |
| 114 | + last_modified = datetime.fromtimestamp(os.path.getmtime(search_index_filepath)).date() |
| 115 | + if ((last_modified - datetime.today().date()).days > self.DAYS_KEEP_CACHE): |
| 116 | + should_generate = True |
| 117 | + except Exception as exc: |
| 118 | + should_generate = True |
| 119 | + |
| 120 | + if not should_generate: |
| 121 | + return False |
| 122 | + |
| 123 | + try: |
| 124 | + opener = kpnet.build_urllib_opener() |
| 125 | + with opener.open("{}{}".format(self.PHPDOC_WEBSITE, self.PHPDOC_SEARCHINDEX_URL)) as request: |
| 126 | + response = request.read() |
| 127 | + except Exception as exc: |
| 128 | + self.err("Could not reach the php documentation website to generate documentation index: ", exc) |
| 129 | + |
| 130 | + data = json.loads(response) |
| 131 | + with open(search_index_filepath, "w") as index_file: |
| 132 | + json.dump(data, index_file, indent=2) |
| 133 | + |
| 134 | + # Read the file containing the documentation index |
| 135 | + def get_documentation_index(self): |
| 136 | + if not self.doc_index: |
| 137 | + with open(self.get_search_index_path(), "r") as index_file: |
| 138 | + data = json.loads(index_file.read()) |
| 139 | + for key in data: |
| 140 | + label = data[key][0] |
| 141 | + description = data[key][1] |
| 142 | + section_type = data[key][2] |
| 143 | + |
| 144 | + doc = self.get_documentation_type(label, description, key, section_type) |
| 145 | + suggestion = self.create_item( |
| 146 | + category=doc.ITEMCAT, |
| 147 | + label=doc.label, |
| 148 | + short_desc=doc.desc, |
| 149 | + target=doc.label, |
| 150 | + data_bag="{}|{}".format(doc.url, doc.section_type), |
| 151 | + args_hint=kp.ItemArgsHint.FORBIDDEN, |
| 152 | + hit_hint=kp.ItemHitHint.IGNORE |
| 153 | + ) |
| 154 | + |
| 155 | + self.doc_index.append(suggestion) |
| 156 | + |
| 157 | + return self.doc_index |
| 158 | + |
| 159 | + # Returns the complete path of the file used as cache to the php documentation search index |
| 160 | + def get_search_index_path(self): |
| 161 | + cache_path = self.get_package_cache_path(True) |
| 162 | + return os.path.join(cache_path, self.INDEX_FILENAME) |
| 163 | + |
| 164 | + def get_documentation_type(self, label, description, url, section_type): |
| 165 | + for cls in Suggestion.__subclasses__(): |
| 166 | + if cls.has_section_type(section_type): |
| 167 | + return cls(label, description, url, section_type) |
| 168 | + raise ValueError |
0 commit comments