検索が出来るようになりました (2025/04/17) | ||
---|---|---|
以前からやりたかった日記の検索が出来るようになりました 「記事検索」にフリーワードを入れると該当する記事一覧が表示されます ![]() |
||
検索結果のリンクをクリックすると該当日記が開きます![]() |
||
ChatGPTに相談するとレンタルサーバーXserverのPythonのバージョンが Python 2.7.5で既にサポートが終わっているからバージョンアップしろとの指示 ところがPython-3.12.3のインストールがエラーで終わらない そうしたらgcc バージョン 4.8.5で古いからgccのバージョンアップをしろとの指示 結局夕方から朝までmakeに時間がかかってしまいました。 |
||
# GCC インストール手順 echo $HOME /home/lily2004 # 作業ディレクトリ作成 mkdir -p $HOME/src cd $HOME/src # MPFR, GMP, MPC のソースをダウンロードしてインストール wget https://ftp.gnu.org/gnu/gmp/gmp-6.2.1.tar.xz tar xf gmp-6.2.1.tar.xz cd gmp-6.2.1 ./configure --prefix=$HOME/local make make install cd .. wget https://www.mpfr.org/mpfr-4.2.1/mpfr-4.2.1.tar.xz tar xf mpfr-4.2.1.tar.xz cd mpfr-4.2.1 ./configure --prefix=$HOME/local --with-gmp=$HOME/local make make install cd .. wget https://ftp.gnu.org/gnu/mpc/mpc-1.2.1.tar.gz tar xf mpc-1.2.1.tar.gz cd mpc-1.2.1 ./configure --prefix=$HOME/local --with-gmp=$HOME/local --with-mpfr=$HOME/local make make install cd .. # GCCのダウンロード wget https://ftp.gnu.org/gnu/gcc/gcc-11.4.0/gcc-11.4.0.tar.xz tar xf gcc-11.4.0.tar.xz cd gcc-11.4.0 # 必要な環境変数の設定(パスの追加など) export PATH=$HOME/local/bin:$PATH export LD_LIBRARY_PATH=$HOME/local/lib:$LD_LIBRARY_PATH export CFLAGS="-I$HOME/local/include" export LDFLAGS="-L$HOME/local/lib" # GCCのビルド準備 ./configure --prefix=$HOME/local --with-gmp=$HOME/local --with-mpfr=$HOME/local --with-mpc=$HOME/local --enable-languages=c,c++ --disable-multilib make make install cd .. # Pythonのインストール準備 wget https://www.python.org/ftp/python/3.12.3/Python-3.12.3.tgz tar xf Python-3.12.3.tgz cd Python-3.12.3 # Pythonの環境設定 export LD_LIBRARY_PATH=$HOME/local/lib64:$LD_LIBRARY_PATH export CFLAGS="-I$HOME/local/include" export LDFLAGS="-L$HOME/local/lib" # Pythonのビルドとインストール ./configure --prefix=$HOME/local --enable-optimizations --with-ensurepip=install make make install cd .. # Pythonの確認 /home/lily2004/local/bin/python3.12 --version # 必要に応じてpipのアップグレード /home/lily2004/local/bin/python3.12 -m ensurepip --upgrade |
||
gccのMakeが終わって作ったCGI search_and_write.cgi # search_and_write.cgi #!/usr/bin/env perl use strict; use warnings; use utf8; use CGI; use JSON; use Encode; binmode(STDOUT, ":encoding(UTF-8)"); my $q = CGI->new; my $keyword = $q->param('keyword') // ''; $keyword = decode('UTF-8', $keyword) if defined $keyword; $keyword =~ s/^\s+|\s+$//g; print $q->header(-type => 'text/html', -charset => 'UTF-8'); print "<html><head><meta charset='UTF-8'><title>検索結果</title></head><body bgc olor=#ffffe0>"; print "<style>table { border-collapse: collapse; } td { padding: 4px 10px; verti cal-align: top; }</style>"; print "<p>キーワード = [$keyword]</p>"; if (!$keyword) { print "<p>キーワードが入力されていません。</p>"; print "</body></html>"; exit; } my $json_file = '/home/lily2004/ji1fgx.com/public_html/index.json'; if (-e $json_file) { open my $fh, '<', $json_file or die "ファイル読み込みエラー: $!"; my $json_data = do { local $/; <$fh> }; close $fh; my $data = decode_json($json_data); my @results = grep { ($_->{title} && $_->{title} =~ /\Q$keyword\E/i) || ($_->{content} && $_->{content} =~ /\Q$keyword\E/i) } @$data; if (@results) { print "<p>検索結果:</p><table border=0 cellpadding=0 cellspacing=0>"; foreach my $result (@results) { my $raw_title = $result->{title}; my $file = $result->{file}; my $path = '/' . $file; # titleにHTMLタグが含まれていたらプレーンテキストだけ取り出す my $title = $raw_title; $title =~ s/<[^>]*>//g; my ($date, $description) = ("", $title); if ($title =~ /[((]([^()()]*)[))]/) { my $paren_content = $1; if ($paren_content =~ /^\d{4}\/\d{2}\/\d{2}$/) { $date = $paren_content; $description =~ s/[((]$date[))]//; } } if ($date ne '') { print "<tr><td><a href=\"$path\" target=\"B\">$date</a></td><td> $description</td></tr>"; } else { print "<tr><td colspan=\"2\"><a href=\"$path\" target=\"B\">$tit le</a></td></tr>"; } } print "</table>"; } else { print "<p>検索結果は見つかりませんでした。</p>"; } } else { print "<p>index.jsonファイルが見つかりません。</p>"; } print "</body></html>"; |
||
目次部分に埋め込まれた<script> <script> window.addEventListener("DOMContentLoaded", function () { const searchBox = document.getElementById("searchBox"); searchBox.addEventListener("keydown", function(event) { if (event.key === "Enter") { event.preventDefault(); // フォームの送信やページリロードを防ぐ const keyword = searchBox.value; console.log("Enterキーが押されました。キーワード:", keyword); const encodedKeyword = encodeURIComponent(keyword); const url = `/script/search_and_write.cgi?keyword=${encodedKeyword}&_=${Date.now()}`; // フレーム名「B」に読み込ませる parent.frames["B"].location.href = url; } }); }); </script> <tr> <td colspan="2"> <form method="get" accept-charset="UTF-8"> <label for="searchBox"><strong>記事検索:</strong></label> <input type="text" id="searchBox" name="keyword" placeholder="キーワードを入力してください" size="35"> <!-- <input type="submit" value="検索">--> </form> <div id="results" style="margin-top: 10px;"></div> </td> </tr> |
||
Json Index (index.json) を作るPythonのプログラムmake_index.2.py cronで1日に一回動かしています 0 2 * * * /home/lily2004/local/bin/python3 /home/lily2004/ji1fgx.com/public_html/script/make_index.2.py /home/lily2004/ji1fgx.com/public_html /home/lily2004/ji1fgx.com/public_html/index.json # make_index.2.py # -*- coding: utf-8 -*- import os import json import sys import chardet from bs4 import BeautifulSoup # ファイルのエンコーディングを判定する関数 def detect_encoding(file_path): with open(file_path, 'rb') as f: raw_data = f.read() result = chardet.detect(raw_data) return result['encoding'] or 'utf-8' # 結果がない場合はutf-8を返す # HTMLから独自タイトルと本文を抽出する関数 def extract_custom_title_and_text(file_path): encoding = detect_encoding(file_path) # エンコーディングを検出 with open(file_path, 'r', encoding=encoding, errors='ignore') as f: html = f.read() soup = BeautifulSoup(html, 'html.parser') # 独自タイトル抽出 th_title = soup.find('th', class_='hpb-cnt-tb-th1') title = '' if th_title: title = th_title.get_text(strip=True) # 本文も抽出(検索用) body = soup.get_text(separator=' ', strip=True) return title, body def main(folder, output_file): index_data = [] for filename in os.listdir(folder): # if filename.endswith('.html', '.php'): if filename.endswith(('.html', '.php')): full_path = os.path.join(folder, filename) try: title, content = extract_custom_title_and_text(full_path) # ファイル名をリンクとして埋め込む link = f'<a href="{filename}" target="B">{filename}</a>' # データをindex_dataに追加 index_data.append({ 'title': title or link, 'file': filename, 'link': link, 'content': content[:300] }) except Exception as e: print(f"Error processing {filename}: {e}") with open(output_file, 'w', encoding='utf-8') as f: json.dump(index_data, f, ensure_ascii=False, indent=2) print(f"Index file created: {output_file}") if __name__ == '__main__': if len(sys.argv) < 3: print("Usage: make_index.py <input_folder> <output_file>") else: input_folder = sys.argv[1] output_file = sys.argv[2] main(input_folder, output_file) |