wget --recursive を使用して index.html を含むディレクトリを作成するにはどうすればよいですか?

wget --recursive を使用して index.html を含むディレクトリを作成するにはどうすればよいですか?

動作やダウンロードの仕方にとても満足していますwget -r

ウェブサイトを提供するローカルホスト サーバーをセットアップしました。ページは次のようになります。

http://localhost:8080/
http://localhost:8080/foo
http://localhost:8080/bar
http://localhost:8080/blog/
http://localhost:8080/blog/1-and-here-the-slug

使用するとwget -r localhost:8080次の構造が作成されます。

.
├── static-files
│   └── ...
├── bar
├── blog
│   └── 1-and-here-the-slug
├── foo
└── index.html

barfoo およびファイルです。これらを、名前が付けられ、リソース (CSS、JS など) へのパスが壊れない、1-and-here-the-slug1 つのファイルを含むディレクトリにしたいと考えています。index.html

私は次のようなものを期待しています:

.
├── static-files
│   └── ...
├── bar
│   └── index.html
├── foo
│   └── index.html
├── blog
│   ├── index.html // <---------- Also I want this one here to show the blog
│   └── 1-and-here-the-slug
│       └── index.html
└── index.html

どうやってやるの?

答え1

http://localhost:8080/blog/1-and-here-the-slug

bar、foo、1-and-here-the-slug はファイルです。これらを、index.html という名前の 1 つのファイルを含むディレクトリにして、リソース (CSS、JS など) へのパスを壊さないようにしたいと思います。

├── blog
│   └── 1-and-here-the-slug
│       └── index.html

http://localhost:8080/blog/1-and-here-the-slug現在のディレクトリがであるときにblog、そのページの名前を に変更するとblog/1-and-here-the-slug/index.html、新しい現在のディレクトリは になりますblog/1-and-here-the-slug。したがって、リソース (CSS、JS) 内の相​​対パスがある場合は、そのパスが壊れます。そしてファイルの内部HTMLを変更せずにこの問題を解決する方法はありません

最善の方法は、拡張子のないファイルの名前を html 拡張子に変更することです。

├── blog
│   └── 1-and-here-the-slug.html
  1. 同じディレクトリを維持しながら、renameコマンドを再帰的に使用できます。

元:

  find tmp -type f ! -name '*.*' | rename -nv 's/(.*)/$1.html/'
  1. 新しいディレクトリを作成することもできますが、相対的なリソースが壊れてしまいます。

元:

  find tmp -type f ! -name '*.*' | while read file; do
       mv $file $file.tmp;
       mkdir $file;
       mv $file.tmp $file/index.html;
 done

ファイルにタグを挿入して<base href="">リソースへの適切なパスを指定することもできますが、これは非常に手間のかかる作業になります。

  1. **または、-Ewgetパラメータを使用する方が良いでしょう

編集: wgetmanページを読むと、2つの素晴らしいオプションが見つかります

  -E
  --adjust-extension
       If a file of type application/xhtml+xml or text/html is downloaded
       and the URL does not end with the regexp \.[Hh][Tt][Mm][Ll]?, this option
       will cause the suffix .html to be appended to the local filename. 

  -k
   --convert-links
       After the download is complete, convert the links in the document to
       make them suitable for local viewing.  This affects not only the visible
       hyperlinks, but any part of the document that links to external content, 
       such as embedded images, links to style sheets, hyperlinks to non-
       HTML content, etc.

関連情報