キーワード検索からオブジェクト検索へ。

 

【Copilotの答えの始め】

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
import re
from bs4 import BeautifulSoup

# HTMLオブジェクトを抽出する関数
def extract_html_objects(base_dir):
    html_objects = []
    for root, _, files in os.walk(base_dir):
        for file in files:
            if file.endswith(".html"):
                file_path = os.path.join(root, file)
                with open(file_path, "r", encoding="utf-8") as f:
                    content_lines = f.readlines()
                    soup = BeautifulSoup("".join(content_lines), "html.parser")
                    # id属性の抽出
                    for tag in soup.find_all(attrs={"id": True}):
                        line_number = next((i + 1 for i, line in enumerate(content_lines) if tag["id"] in line), None)
                        line_content = content_lines[line_number - 1].strip() if line_number else None
                        html_objects.append({
                            "type": "id",
                            "name": tag["id"],
                            "path": file_path,
                            "line_number": line_number,
                            "line_content": line_content
                        })
                    # name属性の抽出
                    for tag in soup.find_all(attrs={"name": True}):
                        line_number = next((i + 1 for i, line in enumerate(content_lines) if tag["name"] in line), None)
                        line_content = content_lines[line_number - 1].strip() if line_number else None
                        html_objects.append({
                            "type": "name",
                            "name": tag["name"],
                            "path": file_path,
                            "line_number": line_number,
                            "line_content": line_content
                        })
    return html_objects

# JavaScriptファイルから関数を抽出し、HTMLオブジェクトを検索する関数
def extract_js_functions_and_search_html(js_base_dir, html_objects):
    js_results = []
    for root, _, files in os.walk(js_base_dir):
        for file in files:
            if file.endswith(".js") or file.startswith("script_"):  # script_フィルター追加
                file_path = os.path.join(root, file)
                with open(file_path, "r", encoding="utf-8") as f:
                    content = f.read()
                    # 関数の抽出
                    functions = re.findall(r'function\s+(\w+)', content)
                    for func in functions:
                        js_results.append({
                            "type": "function",
                            "name": func,
                            "path": file_path,
                            "line_number": content[:content.find(func)].count("\n") + 1,
                            "line_content": content.splitlines()[content[:content.find(func)].count("\n")].strip()
                        })
                    # HTMLの`name`および`id`属性を検索
                    for html_obj in html_objects:
                        if html_obj["name"] in content:
                            js_results.append({
                                "type": html_obj["type"],  # name または id の種類
                                "name": html_obj["name"],
                                "path": file_path,
                                "line_number": content.count("\n", 0, content.find(html_obj["name"])) + 1,
                                "line_content": content.splitlines()[content.count("\n", 0, content.find(html_obj["name"]))].strip()
                            })
    return js_results

# 実行例
html_base_directory = "/path/to/html/files"
js_base_directory = "/path/to/js/files"

html_results = extract_html_objects(html_base_directory)
js_results = extract_js_functions_and_search_html(js_base_directory, html_results)

print("HTML Results:", html_results)
print("JavaScript Results:", js_results)

【Copilotの答えの終わり】

 

 

 

 

軒の薪冬越せますの合図かな