Skip to content
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

Merged
merged 4 commits into from
Apr 11, 2021

Conversation

muryoimpl
Copy link
Contributor

@muryoimpl muryoimpl commented Apr 9, 2021

Nokogiri を使って、_layouts 以下のファイルを解析して、指定した回がなければ追記
するようにしてみました。

Gemfile に 'nokogiri' を追記しているので、netlify へのデプロイ時に bundle install に
オプションを渡せるならば、 group をつけて不要な gem をインストールしないように
したほうがいいかもしれません。

解析後の出力時に既存の html との差分を最小にするため、doctype を大文字にしたり、
meta タグを文字列から削除したり、追記する html 片にスペースを余計に追加したり
しています。

(月が1桁表記なのは少数派だったので、形式は yyyy-mm-dd(曜日), yyyy年mm月dd日(曜日)としました。)

@netlify
Copy link

netlify bot commented Apr 9, 2021

Deploy preview for meetup-kzrb-org ready!

Built with commit 8e678ff

https://deploy-preview-1209--meetup-kzrb-org.netlify.app

_layouts 以下のファイルへの追記忘れをなくすよう、Nokogiri を使って_layouts
にあるファイルを解析し、追加しようとしているイベントの記載がなければ、追記
するようにした。
@muryoimpl muryoimpl force-pushed the add-event-entry-to-layouts-when-generate-index branch from 82e9814 to 8e678ff Compare April 10, 2021 07:20
"##{@number}"
end

# TODO:
Copy link
Member

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

Copy link
Contributor Author

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)

Copy link
Contributor Author

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>

どっちもどっちでぐぬぬな感じ…

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nokogumbo のほうは出力するたび、</body></html> の前に改行が1つ追加されていくので、現状のコードのほうがよさそうでした 😭

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

検証ありがとうございますー!
なるほど〜 、
sparklemotion/nokogiri#1008
nokogumbo、ここらへんで話題になってるんですね。

色々回り回ってたしかに現状のコードのほうが良さそうですね。
お手数おかけして申し訳有りませんでした。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

いえいえ、違う観点からの指摘は大好物です!
「うまくいかない」とか「今のほうがいいかも」につながる情報が新たに得られたので助かりました。ありがとうございます。

@muryoimpl muryoimpl marked this pull request as ready for review April 11, 2021 05:33
@muryoimpl muryoimpl requested review from a team and izawa and removed request for a team April 11, 2021 05:33
@izawa
Copy link
Member

izawa commented Apr 11, 2021

ありがとうございます!ばっちり動いております。
マージしますねー!

@izawa izawa merged commit 2505fdb into master Apr 11, 2021
@izawa izawa deleted the add-event-entry-to-layouts-when-generate-index branch April 11, 2021 05:38
@muryoimpl
Copy link
Contributor Author

ありがとうございます! 🐛 、変更要望があれば PR、issue、ご連絡ください

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants