Skip to content

Commit

Permalink
1.1.0 - refactoring, front matter support
Browse files Browse the repository at this point in the history
  • Loading branch information
MihajloNesic committed Jan 17, 2021
1 parent 7eb92ba commit 20a2a70
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 34 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ or local
{% pdf "/files/pdf/sample.pdf" %}
```

Use front matter
```
---
pdf_file: "/files/pdf/sample.pdf"
---
{% pdf {{ page.pdf_file }} %}
```


Use `no_link` to hide link to pdf file ('View PDF' header)
```
{% pdf "/files/pdf/sample.pdf" no_link %}
Expand All @@ -53,7 +63,7 @@ You can also embed PowerPoint presentations!
{% pdf "http://img.labnol.org/di/PowerPoint.ppt" %}
```

Your file **must** end with `.pdf` or `.ppt`. Everyting else is forbidden.
Your file **must** end with `.pdf`, `.ppt` or `.pptx`. Everyting else is forbidden.

## Result

Expand Down
6 changes: 3 additions & 3 deletions jekyll-pdf-embed.gemspec
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
80 changes: 50 additions & 30 deletions lib/jekyll-pdf-embed.rb
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

1 comment on commit 20a2a70

@MihajloNesic
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solves #2

Please sign in to comment.