-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rake meetup:gen_index 実行時に _layouts 以下のファイルにリンクを追記するようにした #1209
Conversation
Deploy preview for meetup-kzrb-org ready! Built with commit 8e678ff |
_layouts 以下のファイルへの追記忘れをなくすよう、Nokogiri を使って_layouts にあるファイルを解析し、追加しようとしているイベントの記載がなければ、追記 するようにした。
82e9814
to
8e678ff
Compare
"##{@number}" | ||
end | ||
|
||
# TODO: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove_extra_tag()
ですが、そもそも Nokogiri::HTML::Document.parse の段階で、DTD を含めないようにできそうに思います。
具体的には、
Nokogiri::HTML::Document.parse(File.read(path), nil, nil, 4)
で、options に DTDLOAD(Load external subsets) を指定すれば勝手に生成しないようにできるように思いますが、いかがでしょうか?
ref. https://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/ParseOptions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほど parse から勝負が始まっているという発想がなかったです。
https://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/HTML/Document#parse-class_method にあったデフォルトの options に DTDLOAD を足して試してみたのですが、結果変わらずでした。:cry:
options = Nokogiri::XML::ParseOptions.new(
Nokogiri::XML::ParseOptions::DEFAULT_HTML | Nokogiri::XML::ParseOptions::DTDLOAD
)
doc = Nokogiri::HTML::Document.parse(File.read(path), nil, nil, options)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
どうやらNokogiriはhtml5に対応していないらしく、削除したいタグを埋め込みたがるようです。(元のコードでも内部的にはheader
タグも読めていないが NOERROR と RECOVER のオプションで無視されていただけ)
nokogumbo というNokogiri を html5 対応させた gem があり、こちらを使うと削除したいタグは出なくなるのですが、シリアライズ時に以下のようなインデントが消えてしまう問題が出てしまうみたいです。このインデント崩れを許容するならば、今回この差分をとりこんで次回からの差分は li タグの部分だけになります。
-<!DOCTYPE html>
-<html>
- <head>
+<!DOCTYPE html><html><head>
<meta charset="utf-8">
〜〜〜〜〜〜〜〜〜
- </body>
-</html>
+
+
+</body></html>
どっちもどっちでぐぬぬな感じ…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nokogumbo のほうは出力するたび、</body></html>
の前に改行が1つ追加されていくので、現状のコードのほうがよさそうでした 😭
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
検証ありがとうございますー!
なるほど〜 、
sparklemotion/nokogiri#1008
nokogumbo、ここらへんで話題になってるんですね。
色々回り回ってたしかに現状のコードのほうが良さそうですね。
お手数おかけして申し訳有りませんでした。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
いえいえ、違う観点からの指摘は大好物です!
「うまくいかない」とか「今のほうがいいかも」につながる情報が新たに得られたので助かりました。ありがとうございます。
ありがとうございます!ばっちり動いております。 |
ありがとうございます! 🐛 、変更要望があれば PR、issue、ご連絡ください |
Nokogiri を使って、_layouts 以下のファイルを解析して、指定した回がなければ追記
するようにしてみました。
Gemfile に 'nokogiri' を追記しているので、netlify へのデプロイ時に bundle install に
オプションを渡せるならば、 group をつけて不要な gem をインストールしないように
したほうがいいかもしれません。
解析後の出力時に既存の html との差分を最小にするため、doctype を大文字にしたり、
meta タグを文字列から削除したり、追記する html 片にスペースを余計に追加したり
しています。
(月が1桁表記なのは少数派だったので、形式は yyyy-mm-dd(曜日), yyyy年mm月dd日(曜日)としました。)