Github活动供稿网址不包含域

时间:2016-09-23 16:18:31

标签: ruby-on-rails-4 feedjira

我想在rails应用程序中显示用户的活动供稿。我正在使用feedjira。

2.2.4 :006 > xml_feed = Feedjira::Feed.fetch_raw "https://github.com/prasadsurase.atom"
2.2.4 :006 > github_feed = Feedjira::Feed.parse xml_feed
2.2.4 :006 > github_feed.entries.first.content
=> "<!-- pull_request -->\n<svg aria-label=\"Pull request\" class=\"octicon octicon-git-pull-request dashboard-event-icon\" height=\"32\" role=\"img\" version=\"1.1\" viewBox=\"0 0 12 16\" width=\"24\"><path d=\"M11 11.28V5c-.03-.78-.34-1.47-.94-2.06C9.46 2.35 8.78 2.03 8 2H7V0L4 3l3 3V4h1c.27.02.48.11.69.31.21.2.3.42.31.69v6.28A1.993 1.993 0 0 0 10 15a1.993 1.993 0 0 0 1-3.72zm-1 2.92c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2zM4 3c0-1.11-.89-2-2-2a1.993 1.993 0 0 0-1 3.72v6.56A1.993 1.993 0 0 0 2 15a1.993 1.993 0 0 0 1-3.72V4.72c.59-.34 1-.98 1-1.72zm-.8 10c0 .66-.55 1.2-1.2 1.2-.65 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2zM2 4.2C1.34 4.2.8 3.65.8 3c0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2z\"></path></svg>\n\n<div class=\"time\">\n  <relative-time datetime=\"2016-09-23T07:30:24Z\">Sep 23, 2016</relative-time>\n</div>\n\n<div class=\"title\">\n  <a href=\"/prasadsurase\" data-ga-click=\"News feed, event click, Event click type:PullRequestEvent target:actor\">prasadsurase</a> opened pull request <a href=\"/joshsoftware/code-curiosity/pull/136\" data-ga-click=\"News feed, event click, Event click type:PullRequestEvent target:pull\">joshsoftware/code-curiosity#136</a>\n</div>\n\n<div class=\"details\">\n  <a href=\"/prasadsurase\"><img alt=\"@prasadsurase\" class=\"gravatar\" height=\"30\" src=\"https://avatars3.githubusercontent.com/u/562052?v=3&amp;s=60\" width=\"30\" /></a>\n  <div class=\"message\">\n    <blockquote>Fixed controller method scopes. Removed unwanted routes.</blockquote>\n      <div class=\"pull-info\">\n        <svg aria-hidden=\"true\" class=\"octicon octicon-git-commit\" height=\"16\" version=\"1.1\" viewBox=\"0 0 14 16\" width=\"14\"><path d=\"M10.86 7c-.45-1.72-2-3-3.86-3-1.86 0-3.41 1.28-3.86 3H0v2h3.14c.45 1.72 2 3 3.86 3 1.86 0 3.41-1.28 3.86-3H14V7h-3.14zM7 10.2c-1.22 0-2.2-.98-2.2-2.2 0-1.22.98-2.2 2.2-2.2 1.22 0 2.2.98 2.2 2.2 0 1.22-.98 2.2-2.2 2.2z\"></path></svg>\n        <em>1</em> commit with\n        <em>34</em> additions and\n        <em>31</em> deletions\n      </div>\n  </div>\n</div>\n"

在haml中原始 github_feed.entries 呈现每个条目内容时,链接会执行不包含域 https://github.com/ ,但仅包含路径。这会导致问题,即UI上的呈现链接包含应用程序域而不是github。我们如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

由于github atom feeds为内容提供了相对url,你可能想用绝对引用替换它们。

这可以通过

完成
^((?:\s*\d{1,3}[wW]\s*)?(?:\s+\d{1,3}[dD]\s*)?(?:\s+\d{1,3}[hH]\s*)?(?:\s+\d{1,3}[mM]\s*)?(?:\s+\d{1,3}[sS]\s*)?)$

然后替换你的:

 NSPredicate *predicate = [NSPredicate predicateWithFormat: @"compositeName CONTAINS[c] %@", searchText];

gsub( %r{<a href=\"/},'<a href="https://github.com/')