Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add single quotes option #336

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/fontcustom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def gem_lib
:autowidth => false,
:no_hash => false,
:css3 => false,
:single_quotes => false,
:debug => false,
:force => false,
:quiet => false,
Expand Down
3 changes: 3 additions & 0 deletions lib/fontcustom/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class CLI < Thor
class_option :css3, :type => :boolean,
:desc => "Use CSS3 Pseudo Elements"

class_option :single_quotes, :aliases => %w|--signle-quotes -Q|, :type => :boolean,
:desc => "Use single quotes in generated CSS and SCSS templates"

class_option :no_hash, :aliases => "-h", :type => :boolean,
:desc => "Generate fonts without asset-busting hashes."

Expand Down
44 changes: 26 additions & 18 deletions lib/fontcustom/generator/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def initialize(manifest)
@pseudo_element = '::before';
end

@quote = '"'
if @options[:single_quotes]
@quote = "'"
end
end

def generate
Expand Down Expand Up @@ -143,30 +147,30 @@ def font_face(style = {})
# With and without Base64
if @options[:base64]
string = %Q|@font-face {
font-family: "#{font_name}";
src: #{url}("#{path}.eot?") format("embedded-opentype");
font-family: #{quote}#{font_name}#{quote};
src: #{url}(#{quote}#{path}.eot?#{quote}) format(#{quote}embedded-opentype#{quote});
font-weight: normal;
font-style: normal;
}

@font-face {
font-family: "#{font_name}";
src: url("data:application/x-font-woff;charset=utf-8;base64,#{woff_base64}") format("woff"),
#{url}("#{path}.woff2") format("woff2"),
#{url}("#{path}.ttf") format("truetype"),
#{url}("#{path}.svg##{font_name}") format("svg");
font-family: #{quote}#{font_name}#{quote};
src: url(#{quote}data:application/x-font-woff;charset=utf-8;base64,#{woff_base64}#{quote}) format(#{quote}woff#{quote}),
#{url}(#{quote}#{path}.woff2#{quote}) format(#{quote}woff2#{quote}),
#{url}(#{quote}#{path}.ttf#{quote}) format(#{quote}truetype#{quote}),
#{url}(#{quote}#{path}.svg##{font_name}#{quote}) format(#{quote}svg#{quote});
font-weight: normal;
font-style: normal;
}|
else
string = %Q|@font-face {
font-family: "#{font_name}";
src: #{url}("#{path}.eot");
src: #{url}("#{path}.eot?#iefix") format("embedded-opentype"),
#{url}("#{path}.woff2") format("woff2"),
#{url}("#{path}.woff") format("woff"),
#{url}("#{path}.ttf") format("truetype"),
#{url}("#{path}.svg##{font_name}") format("svg");
font-family: #{quote}#{font_name}#{quote};
src: #{url}(#{quote}#{path}.eot#{quote});
src: #{url}(#{quote}#{path}.eot?#iefix#{quote}) format(#{quote}embedded-opentype#{quote}),
#{url}(#{quote}#{path}.woff2#{quote}) format(#{quote}woff2#{quote}),
#{url}(#{quote}#{path}.woff#{quote}) format(#{quote}woff#{quote}),
#{url}(#{quote}#{path}.ttf#{quote}) format(#{quote}truetype#{quote}),
#{url}(#{quote}#{path}.svg##{font_name}#{quote}) format(#{quote}svg#{quote});
font-weight: normal;
font-style: normal;
}|
Expand All @@ -177,8 +181,8 @@ def font_face(style = {})

@media screen and (-webkit-min-device-pixel-ratio:0) {
@font-face {
font-family: "#{font_name}";
src: #{url}("#{path}.svg##{font_name}") format("svg");
font-family: #{quote}#{font_name}#{quote};
src: #{url}(#{quote}#{path}.svg##{font_name}#{quote}) format(#{quote}svg#{quote});
}
}|
string
Expand All @@ -198,7 +202,7 @@ def glyph_selectors

def glyph_properties
%Q| display: inline-block;
font-family: "#{font_name}";
font-family: #{quote}#{font_name}#{quote};
font-style: normal;
font-weight: normal;
font-variant: normal;
Expand All @@ -213,14 +217,18 @@ def glyph_properties

def glyphs
output = @glyphs.map do |name, value|
%Q|#{@options[:css_selector].sub('{{glyph}}', name.to_s)}#{@pseudo_element} { content: "\\#{value[:codepoint].to_s(16)}"; }|
%Q|#{@options[:css_selector].sub('{{glyph}}', name.to_s)}#{@pseudo_element} { content: #{quote}\\#{value[:codepoint].to_s(16)}#{quote}; }|
end
output.join "\n"
end

def pseudo_element
@pseudo_element
end

def quote
@quote
end
end
end
end
2 changes: 2 additions & 0 deletions lib/fontcustom/templates/fontcustom.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
# Copyright information.
#copyright:

# Use single quotes in generated CSS and SCSS templates (default false)
# single_quotes: true

# -----------------------------------------------------------------------------
# Input / Output Locations
Expand Down
18 changes: 18 additions & 0 deletions spec/fontcustom/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,23 @@
expect(File.read(css)).to match("../foo/bar/")
end
end

context 'single quotes' do
it "should generate fonts and templates with single quotes" do
live_test do |testdir|
Fontcustom::CLI.start ["compile", "vectors", "--templates", "preview", "css", "scss-rails", "--single-quotes"]
preview = File.join testdir, "fontcustom", "fontcustom-preview.html"

expect(Dir.glob(File.join(testdir, "fontcustom", "fontcustom_*\.{ttf,svg,woff,eot}")).length).to eq(4)
expect(File.exists?(preview)).to be_truthy

generated_css = File.read Dir.glob(File.join(testdir, "fontcustom", "*.css")).first
expect(generated_css.scan('"').count).to be 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

Your assertions don't match the test. Can you add an assertion that confirms single quotes are present, rather than testing the lack of double quotes?

Copy link
Author

@AlaaSarhan AlaaSarhan Jul 13, 2017

Choose a reason for hiding this comment

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

The easiest way to assert that all " were replaced with ' is to assert there are no ". Of course that does not assert that there are any ', or even worse to assert they are replacing what was supposed to be " (is there an assertion actually for that in other specs?). I don't know how to assert that the right amount of ' were replaced, and whether they were put in the right places.

I don't know how to do that, except by parsing the generated css for a valid CSS along with the current assertion of no " present (over-engineered, otherwise would've done it already). Any other idea?


generated_scss = File.read Dir.glob(File.join(testdir, "fontcustom", "*.scss")).first
expect(generated_scss.scan('"').count).to be 0
end
end
end
end
end