-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.1.0 - refactoring, front matter support
- Loading branch information
1 parent
7eb92ba
commit 20a2a70
Showing
3 changed files
with
64 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
Gem::Specification.new do |s| | ||
s.name = 'jekyll-pdf-embed' | ||
s.version = '1.0.4' | ||
s.version = '1.1.0' | ||
s.authors = ["Mihajlo Nesic"] | ||
s.email = 'nesicmihajlo98@gmail.com' | ||
|
||
s.date = '2020-03-27' | ||
s.date = '2021-01-17' | ||
s.summary = "Jekyll plugin for embedding PDF files to any page or post" | ||
s.description = %q{ | ||
Jekyll PDF Embed is a ruby gem for Jekyll static site generator. | ||
It allows user to easily embed external or local PDF files to any page or blog post. | ||
} | ||
s.files = ["lib/jekyll-pdf-embed.rb"] | ||
s.homepage = 'https://github.com/MihajloNesic/jekyll-pdf-embed' | ||
s.license = 'MIT' | ||
s.license = 'GPL-3.0' | ||
|
||
s.add_dependency 'jekyll' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,64 @@ | ||
require "jekyll" | ||
|
||
class PDFEmbed < Liquid::Tag | ||
def initialize(tag_name, text, tokens) | ||
class PDFEmbedTest < Liquid::Tag | ||
|
||
def initialize(tagName, content, tokens) | ||
super | ||
@allowed_files = [".pdf\"", ".ppt\""] | ||
@link = text[/".*?"/] | ||
@link_escaped = @link.gsub(" ", "%20") | ||
@raw_link = @link_escaped.gsub("\"", "") | ||
@ext = File.extname(@link_escaped) | ||
@is_allowed = @allowed_files.include? @ext | ||
@text = text.gsub(@link, @link_escaped) | ||
*@params = @text.split(/ /) | ||
@content = content | ||
|
||
# define allowed extensions | ||
@allowed_files = [".pdf", ".ppt", ".pptx"] | ||
|
||
# get the 'no_link' param and check if it is present | ||
@param = @content.split(/ /).last | ||
@no_link = @param == 'no_link' ? true : false | ||
end | ||
|
||
def render(context) | ||
if @no_link | ||
# if 'no_link' is present then | ||
# remove only the last occurence of 'no_link' keyword | ||
# https://www.regular-expressions.info/keep.html | ||
@content = @content.sub(/.*\K no_link/, '') | ||
end | ||
|
||
@link = "#{context[@content.strip]}" | ||
|
||
# get extension and check if it is allowed | ||
@extension = File.extname(@link) | ||
@is_allowed = @allowed_files.include? @extension | ||
|
||
if !@is_allowed | ||
raise ArgumentError, 'ERROR:file_not_allowed' | ||
raise ArgumentError, 'ERROR:file_not_allowed -> ' + @link | ||
end | ||
|
||
if @ext == ".pdf\"" | ||
if @params.length == 1 | ||
%Q{<style>.pdf-embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; margin-bottom: 20px; border-style: solid; } .pdf-link { background-color: white; text-align: center; border-style: solid; } .pdf-embed-container iframe, .pdf-embed-container object, .pdf-embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }</style><div class='pdf-link'><a href=#{@link_escaped} target="_blank">View PDF</a></div><div class='pdf-embed-container'><iframe title="PDF file" width="640" height="390" src=#{@link_escaped} frameborder="0" allowfullscreen></iframe></div>} | ||
elsif @params.length == 2 && @params[1] == 'no_link' | ||
%Q{<style>.pdf-embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; margin-bottom: 20px; } .pdf-embed-container iframe, .pdf-embed-container object, .pdf-embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }</style><div class='pdf-embed-container'><iframe title="PDF file" width="640" height="390" src=#{@link_escaped} frameborder="0" allowfullscreen></iframe></div>} | ||
|
||
if @extension == ".pdf" | ||
if @no_link | ||
%Q{<style>.pdf-embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; margin-bottom: 20px; } .pdf-embed-container iframe, .pdf-embed-container object, .pdf-embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }</style><div class='pdf-embed-container'><iframe title="PDF file" width="640" height="390" src="#{@link}" frameborder="0" allowfullscreen></iframe></div>} | ||
else | ||
raise ArgumentError, 'ERROR:bad_syntax' | ||
%Q{<style>.pdf-embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; margin-bottom: 20px; border-style: solid; } .pdf-link { background-color: white; text-align: center; border-style: solid; } .pdf-embed-container iframe, .pdf-embed-container object, .pdf-embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }</style><div class='pdf-link'><a href="#{@link}" target="_blank">View PDF</a></div><div class='pdf-embed-container'><iframe title="PDF file" width="640" height="390" src="#{@link}" frameborder="0" allowfullscreen></iframe></div>} | ||
end | ||
elsif @ext == ".ppt\"" | ||
if @params.length == 1 | ||
%Q{<style>.pdf-embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; margin-bottom: 20px; border-style: solid; } .pdf-link { background-color: white; text-align: center; border-style: solid; } .pdf-embed-container iframe, .pdf-embed-container object, .pdf-embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }</style><div class='pdf-link'><a href="https://view.officeapps.live.com/op/embed.aspx?src=#{@raw_link}" target="_blank">View presentation</a></div><div class='pdf-embed-container'><iframe title="PDF file" width="640" height="390" src="https://view.officeapps.live.com/op/embed.aspx?src=#{@raw_link}" frameborder="0" allowfullscreen></iframe></div>} | ||
elsif @params.length == 2 && @params[1] == 'no_link' | ||
%Q{<style>.pdf-embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; margin-bottom: 20px; } .pdf-embed-container iframe, .pdf-embed-container object, .pdf-embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }</style><div class='pdf-embed-container'><iframe title="PDF file" width="640" height="390" src="https://view.officeapps.live.com/op/embed.aspx?src=#{@raw_link}" frameborder="0" allowfullscreen></iframe></div>} | ||
elsif @extension == ".ppt" or @extension == ".pptx" | ||
|
||
# checks if the presentation is not on remote address | ||
if !@link.include? "http://" and !@link.include? "https://" | ||
# get base url and appent file location to it | ||
@baseurl = "#{context.registers[:site].config['url']}" | ||
@link = "https://view.officeapps.live.com/op/embed.aspx?src=#{@baseurl}#{@link}" | ||
# locally, this will not work | ||
# but once the Jekyll site is hosted remotely, the baseurl will not be 'localhost' | ||
else | ||
raise ArgumentError, 'ERROR:bad_syntax' | ||
@link = "https://view.officeapps.live.com/op/embed.aspx?src=#{@link}" | ||
end | ||
|
||
if @no_link | ||
%Q{<style>.pdf-embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; margin-bottom: 20px; } .pdf-embed-container iframe, .pdf-embed-container object, .pdf-embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }</style><div class='pdf-embed-container'><iframe title="PDF file" width="640" height="390" src="#{@link}" frameborder="0" allowfullscreen></iframe></div>} | ||
else | ||
%Q{<style>.pdf-embed-container { position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%; margin-bottom: 20px; border-style: solid; } .pdf-link { background-color: white; text-align: center; border-style: solid; } .pdf-embed-container iframe, .pdf-embed-container object, .pdf-embed-container embed { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }</style><div class='pdf-link'><a href="#{@link}" target="_blank">View presentation</a></div><div class='pdf-embed-container'><iframe title="Presentation file" width="640" height="390" src="#{@link}" frameborder="0" allowfullscreen></iframe></div>} | ||
end | ||
else | ||
raise ArgumentError, 'ERROR:bad_syntax' | ||
end | ||
end | ||
|
||
end | ||
|
||
Liquid::Template.register_tag('pdf', self) | ||
end | ||
end |
20a2a70
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solves #2