从给定的url生成诸如youtube或vimeo之类的服务的嵌入(html)代码。规则在插件中指定。
vishap的Python项目详细描述
从给定的url生成诸如youtube或vimeo之类的服务的嵌入(html)代码。 规则在插件中指定。
提供以下服务的插件(按字母顺序列出):
- 维梅奥
- YouTube
安装
使用来自pypi的最新稳定版本安装:
$ pip install vishap
或者从BitBucket安装最新的稳定版本:
$ pip install -e hg+https://bitbucket.org/barseghyanartur/vishap@stable#egg=vishap
或者从github安装最新的稳定版本:
$ pip install -e git+http://github.org/barseghyanartur/vishap@stable#egg=vishap
就这些。有关详细信息,请参见Usage and examples
部分。
用法和示例
简单用法
必需的导入
fromvishapimportrender_video
渲染vimeo代码
以下代码:
printrender_video('http://vimeo.com/45655450',500,281)
将产生以下输出:
<iframesrc="//player.vimeo.com/video/45655450"width="500"height="281"frameborder="0"webkitallowfullscreenmozallowfullscreenallowfullscreen></iframe>
呈现youtube代码
以下代码:
printrender_video('http://www.youtube.com/watch?v=LIPl7PtGXNI',560,315)
将产生以下输出:
<iframewidth="560"height="315"src="//www.youtube.com/embed/LIPl7PtGXNI"frameborder="0"allowfullscreen></iframe>
填充可用区域
如果希望嵌入代码填充可用区域,请在 width和height属性,并将其放在容器中 会伸展。
示例:
s="""<div class="video-wrapper"> {embed_code} </div>""".format(embed_code=render_video('http://www.youtube.com/watch?v=LIPl7PtGXNI'))
然后将产生以下输出:
<iframesrc="//www.youtube.com/embed/LIPl7PtGXNI"frameborder="0"allowfullscreen></iframe>
然后,您的css文件应该类似于以下内容
.video-wrapper{width:600px;height:500px;padding:0;}.video-wrapperiframe{position:absolute;width:100%;height:100%;}
注册自定义插件
classExamplePlugin(BaseVideoPlugin):uid="example"name="Example"url_pattern="^(?P<prefix>(http\:\/\/www\.youtube\.com\/watch\?v=)|(http\:\/\/www\.youtube\.com\/v\/)|(http\:\/\/youtu\.be\/))(?P<value>[A-Za-z0-9\-=_]{11})"id_pattern="^(?P<value>[A-Za-z0-9\-=_]{11})"thumbnail_pattern="//img.youtube.com/vi/{0}/{1}.jpg"embed_code=""" <iframe src="//www.youtube.com/embed/{video_id}" {options} frameborder="0" allowfullscreen></iframe> """plugin_registry.register(ExamplePlugin)
< H3>替换现有插件
如果你想替换/更新现有的插件,你可以强制注册它 登记处。
classUpdatedYoutubePlugin(BaseVideoPlugin):uid="youtube"name="Youtube"url_pattern="^(?P<prefix>(http\:\/\/www\.youtube\.com\/watch\?v=)|(http\:\/\/www\.youtube\.com\/v\/)|(http\:\/\/youtu\.be\/))(?P<value>[A-Za-z0-9\-=_]{11})"id_pattern="^(?P<value>[A-Za-z0-9\-=_]{11})"thumbnail_pattern="//img.youtube.com/vi/{0}/{1}.jpg"embed_code=""" <iframe src="//www.youtube.com/embed/{video_id}" {options} frameborder="0" allowfullscreen></iframe> """plugin_registry.register(UpdatedYoutubePlugin,force=True)
django集成
安装
将vishap.contrib.apps.django.vishap
添加到INSTALLED_APPS
INSTALLED_APPS=(# ...'vishap.contrib.apps.django.vishap',# ...)
使用量
在您的模板中:
示例1:
{% load vishap_tags %} {% render_video 'http://www.youtube.com/watch?v=LIPl7PtGXNI' 560 315 as rendered_video_example_1 %} {{ rendered_video_example_1|safe }}
示例2:
{% load vishap_tags %} {% with video_url='http://vimeo.com/41055612' video_width='500' video_height='281' %} {% render_video video_url video_width video_height as rendered_video_example_2 %} {{ rendered_video_example_2|safe }} {% endwith %}
缺少插件?
缺少您喜爱的服务的插件?对项目的贡献 制作一个,它将出现在一个新的版本(将发布非常 或者请求一个特性。
许可证
gpl 2.0/lgpl 2.1
支架
如有任何问题,请通过Author
部分中给出的电子邮件与我联系。