Jekyll扩展程序调用外部脚本 [英] Jekyll extension calling an external script
问题描述
我编写了一个简单的Jekyll插件,使用twitter gem(请参见下文)提取我的推文.我想将插件的ruby脚本保留在我打开的Github网站上,但是在对twitter API进行了最新更改之后,该gem现在需要身份验证凭据.
I've written a simple Jekyll plugin to pull in my tweets using the twitter gem (see below). I'd like to keep the ruby script for the plugin on my open Github site, but following recent changes to the twitter API, the gem now requires authentication credentials.
require 'twitter' # Twitter API
require 'redcarpet' # Formatting links
module Jekyll
class TwitterFeed < Liquid::Tag
def initialize(tag_name, text, tokens)
super
input = text.split(/, */ )
@user = input[0]
@count = input[1]
if input[1] == nil
@count = 3
end
end
def render(context)
# Initialize a redcarpet markdown renderer to autolink urls
# Could use octokit instead to get GFM
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML,
:autolink => true,
:space_after_headers => true)
## Attempt to load credentials externally here:
require '~/.twitter_auth.rb'
out = "<ul>"
tweets = @client.user_timeline(@user)
for i in 0 ... @count.to_i
out = out + "<li>" + markdown.render(tweets[i].text) +
" <a href=\"http://twitter.com/" + @user + "/statuses/" +
tweets[i].id.to_s + "\">" + tweets[i].created_at.strftime("%I:%M %Y/%m/%d") +
"</a> " + "</li>"
end
out + "</ul>"
end
end
end
Liquid::Template.register_tag('twitter_feed', Jekyll::TwitterFeed)
如果我替换行
require '~/.twitter_auth.rb'
其中twitter_auth.rb
包含以下内容:
require 'twitter'
@client = Twitter::Client.new(
:consumer_key => "CEoYXXXXXXXXXXX",
:consumer_secret => "apnHXXXXXXXXXXXXXXXXXXXXXXXX",
:oauth_token => "105XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
:oauth_token_secret => "BJ7AlXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
)
如果我将这些内容直接放在上面的脚本中,那么我的插件脚本就可以正常工作.但是,当我将它们移动到外部文件并尝试如图所示读取它们时,Jekyll无法进行身份验证.当我从irb
调用该函数时,该函数似乎正常工作,因此我不确定为什么在Jekyll构建期间该函数不起作用.
If I place these contents directly into the script above, then my plugin script works just fine. But when I move them to an external file and try to read them in as shown, Jekyll fails to authenticate. The function seems to work just fine when I call it from irb
, so I am not sure why it does not work during the Jekyll build.
推荐答案
我认为您可能对require
的工作方式感到困惑.当您调用require
时,首先Ruby检查是否已经需要该文件,如果是,则直接返回.如果尚未运行,则将运行文件的内容,但运行范围与require
语句不同.换句话说,使用require
不是等同于用文件内容替换require
语句(例如,是,C的
I think that you may be confused about how require
works. When you call require
, first Ruby checks if the file has already been required, if so it just returns directly. If it hasn’t then the contents of the file are run, but not in the same scope as the require
statement. In other words using require
isn’t the same as replacing the require
statement with the contents of the file (which is how, for example, C’s #include
works).
在您的情况下,当您需要~/.twitter_auth.rb
文件时,将创建@client
实例变量,但将其作为顶级main
对象的实例变量,而将 not 用作TwitterFeed
实例的实例变量,其中require
被称为form.
In your case, when you require your ~/.twitter_auth.rb
file, the @client
instance variable is being created, but as an instance variable of the top level main
object, not as an instance variable of the TwitterFeed
instance where require
is being called form.
您可以执行类似的操作,将Twitter::Client
对象分配给一个常量,然后可以从render
方法引用该常量:
You could do something like assign the Twitter::Client
object to a constant that you could then reference from the render
method:
MyClient = Twitter::Client.new{...
然后
require '~/twitter_auth.rb'
@client = MyClient
...
我仅建议这是对require
所发生情况的解释,这并不是一种很好的技术.
I only suggest this as an explanation of what’s happening with require
, it’s not really a good technique.
我认为,更好的选择是将凭据以简单的数据格式保存在主目录中,然后从脚本中读取它们并使用它们创建Twitter客户端.在这种情况下,Yaml可能会胜任.
A better option, I think, would be to keep your credentials in a simple data format in your home directory, then read them form your script and create the Twitter client with them. In this case Yaml would probably do the job.
首先用看起来像这样的~/twitter_auth.yaml
替换您的~/twitter_auth.rb
:
First replace your ~/twitter_auth.rb
with a ~/twitter_auth.yaml
that looks soemthing like:
:consumer_key: "CEoYXXXXXXXXXXX"
:consumer_secret: "apnHXXXXXXXXXXXXXXXXXXXXXXXX"
:oauth_token: "105XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
:oauth_token_secret: "BJ7AlXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
然后在班级中有requre "~/twitter_auth.rb"
的地方替换为该文件(在文件顶部还需要require 'yaml'
):
Then where you have requre "~/twitter_auth.rb"
in your class, replace with this (you’ll also need require 'yaml'
at the top of the file):
@client = Twitter::Client.new(YAML.load_file("~/twitter_auth.yaml"))
这篇关于Jekyll扩展程序调用外部脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!