diff options
| author | Omar Roth <omarroth@hotmail.com> | 2018-03-06 22:00:35 -0600 |
|---|---|---|
| committer | Omar Roth <omarroth@hotmail.com> | 2018-03-06 22:00:35 -0600 |
| commit | ebe51c91d762a3e9c4804fe04ee81b81da64967b (patch) | |
| tree | 62e6416406eeef76e6ad26fe4146739b79c4363e /src/helpers.cr | |
| parent | e9f214cdc015e00a7b3e04eb73a735b046fd7014 (diff) | |
| download | invidious-ebe51c91d762a3e9c4804fe04ee81b81da64967b.tar.gz invidious-ebe51c91d762a3e9c4804fe04ee81b81da64967b.tar.bz2 invidious-ebe51c91d762a3e9c4804fe04ee81b81da64967b.zip | |
Add local alternatives for video links
Diffstat (limited to 'src/helpers.cr')
| -rw-r--r-- | src/helpers.cr | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/helpers.cr b/src/helpers.cr index 2d2b9337..fae9653e 100644 --- a/src/helpers.cr +++ b/src/helpers.cr @@ -290,6 +290,9 @@ def template_comments(root) score = child["data"]["score"] body_html = HTML.unescape(child["data"]["body_html"].as_s) + # Replace local links wtih links back to Reddit + body_html = fill_links(body_html, "https", "www.reddit.com") + replies_html = "" if child["data"]["replies"] != "" replies_html = template_comments(child["data"]["replies"]["data"]["children"]) @@ -341,3 +344,48 @@ def arg_array(array) return args end + +def add_alt_links(html) + alt_links = [] of {Int32, String} + + # This is painful but is likely the only way to accomplish this in Crystal, + # as Crystigiri and others are not able to insert XML Nodes into a document. + # The goal here is to use as little regex as possible + html.scan(/<a[^>]*>([^<]+)<\/a>/) do |match| + anchor = XML.parse_html(match[0]) + anchor = anchor.xpath_node("//a").not_nil! + url = URI.parse(HTML.unescape(anchor["href"])) + + if ["www.youtube.com", "youtu.be", "m.youtube.com"].includes?(url.host) && url.path == "/watch" + alt_link = <<-END_HTML + <a class="link" href="#{url.full_path}"> + <i class="fa fa-link" aria-hidden="true"></i> + </a> + END_HTML + + alt_links << {match.end.not_nil!, alt_link} + end + end + + alt_links.reverse! + alt_links.each do |position, alt_link| + html = html.insert(position, alt_link) + end + + return html +end + +def fill_links(html, scheme, host) + html = XML.parse_html(html) + + html.xpath_nodes("//a").each do |match| + url = URI.parse(match["href"]) + if !url.host # If reddit link + url.scheme = scheme + url.host = host + match["href"] = url + end + end + + html = html.to_xml +end |
