I'd like to use full html syntax with Slim. I don't want the cryptography they offer. But I think it's impossible. Here's my html code and Slim throws an error.
The target:
I want not to output an additional class of div element if some condition is met:
<div class="foo bar"></div> # <-- standard
<div class="foo"></div> # <-- no "bar" class if some condition is met
And here is my Slim code:
<body> # <-- 11th line
- (1..5).each do |i| # <-- The dash means a line of Ruby code...
<div class="foo # <-- ...after it an indentation of lines follows
- if(i != 2) # <-- if "i" is not 2
= bar # <-- output "bar" as additional class for the div
= " data-co="#{i}"> # <-- indentation back, I want an output of the rest
</div>
</body> # <-- 17th line
</html> # <-- 19th line, the last element
Logically everything is all right. All indentations and = and - are obeyed. But I get error:
!! Unexpected error while processing request: tmpl.slim:16: syntax error, unexpected keyword_end, expecting ')
'
; end; _buf << ("</div>");
^
tmpl.slim:17: syntax error, unexpected keyword_end, expecting ')'
; end; _buf << ("</body>"\
^
tmpl.slim:22: syntax error, unexpected keyword_end, expecting ')'
end;end;end;end
I have two questions:
1) Is it possible to fully use html syntax in Slim?
2) How may I solve my issue?
I think you are thinking this the wrong way. If your end goal is to produce two types of opening tags based on one condition, then a simple if else statement will work, which is sort-of what you did. But, because you were writing in multiple lines in an HTML opening tag, SLIM freaked out. Try this out and let me know what happens.
<body>
- (1..5).each do |i|
- if i.eql? 2
<div class="foo">
- else
<div class="foo bar" data-co="#{i}">
</div>
</body>
Related
I have found a Ruby plugin for Jekyll to obfuscate an email address like this in a Jekyll webpage with Liquid
{{ site.email | mailObfuscate }}
However, I would like to pass multiple params to mailObfuscate
I have tried the following
{{ email=site.email, linkText='foo bar' | mailObfuscate }}
However, this gives an error when building my site
Liquid Warning: Liquid syntax error (line 89): Unexpected character = in "{{ email=site.email, linkText='foo bar' | mailObfuscate }}" in privacy.html
Liquid Exception: undefined method gsub' for nil:NilClass in privacy.html
Error: undefined methodgsub' for nil:NilClass
Error: Run jekyll build --trace for more information.
Running the trace gives the following error
1: from D:/Ruby26-x64/lib/ruby/2.6.0/uri/common.rb:103:in escape'
D:/Ruby26-x64/lib/ruby/2.6.0/uri/rfc2396_parser.rb:305:inescape':
undefined method `gsub' for nil:NilClass (NoMethodError)
The complete trace can be found at Pastebin
How can I pass multiple variables?
You need to modify the method to take a 2nd argument, then you can use it as the link text. Try this:
require "base64"
require "uri"
module ObfuscateMailAddress
def mailObfuscate(email_address, link_text )
base64Mail = Base64.strict_encode64(URI::encode(email_address))
# See http://techblog.tilllate.com/2008/07/20/ten-methods-to-obfuscate-e-mail-addresses-compared/
output = "<a href=\"#\" "
output += "data-contact=\"#{base64Mail}\" target=\"_blank\" "
output += "onfocus=\"this.href = 'mailto:' + atob(this.dataset.contact)\">"
output += "<script type=\"text/javascript\">document.write(atob(\"#{base64Mail}\"));</script>#{link_text}</a>"
return output
end
end
Liquid::Template.register_filter(ObfuscateMailAddress)
To pass multiple arguments in your liquid template, the syntax is a bit strange, see documentation. The string on the left side of the pipe automatically get's passed as the first argument to your ruby method, while additional arguments get passed with a colon.
{{ 'test#example.com' | mailObfuscate:'myLinkText' }}
But you also, if you are on Ruby >= 2.3, you can make your method more readable with no need for all the escape characters and better syntax highlighting in your editor if you change your method to use SQUIGGLY HEREDOC for your string definition, explicit return is not required in anycase. For Ruby < 2.3 you can still use regular HEREDOC just replace ~ with - but you just have extra indentation in your string, which is no problem anyway for rendered html.
def mailObfuscate(email_address, link_text )
base64Mail = Base64.strict_encode64(URI::encode(email_address))
ouput = <<~HTML
<a href="#" data-contact="#{base64Mail}" target="_blank"
onfocus="this.href = 'mailto:' + atob(this.dataset.contact)" >
<script type="text/javascript">
document.write(atob("#{base64Mail}"));
</script>
#{link_text}
</a>
HTML
end
And when it is called like this:
puts mailObfuscate('foo#bar.com', 'foobar')
It will render:
<a href="#" data-contact="Zm9vQGJhci5jb20=" target="_blank"
onfocus="this.href = 'mailto:' + atob(this.dataset.contact)" >
<script type="text/javascript">
document.write(atob("Zm9vQGJhci5jb20="));
</script>
foobar
</a>
As a side note, ruby style guide recommends we use snake_case for method names so you might wanna use mail_obfuscate for your method name instead.
I'm writing an extension for Redcarpet for a Jekyll-powered website. I want to use {x|y} as a tag in markdown that evaluates to the HTML <ruby> tag (and its associates). I wrote this class as per Jekyll's guide, Redcarpet's guide, and this guide on how to do so:
class Jekyll::Converters::Markdown::HotelDown < Redcarpet::Render::HTML
def preprocess(doc)
s = "<ruby><rb>\\1</rb><rp>(</rp><rt>\\2</rt><rp>)</rp></ruby>"
doc.gsub!(/\[([\s\S]+)\|([\s\S]+)\]/, s)
doc
end
end
But, I seem to be getting a couple errors when I run bundle exec jekyll serve:
Configuration file: C:/Users/Alex/OneDrive/codes/hotelc.me/hotelc.me/_config.yml
plugin_manager.rb:58:in `require': HotelDown.rb:4: syntax error, unexpected tIDENTIFIER, expecting ')' (SyntaxError)
doc.gs-ub!(/\[([\-s\S]+)\|([-\s\S]+)\]/-, s)
^
HotelDown.rb:4: syntax error, unexpected ')', expecting '='
doc.gs-ub!(/\[([\-s\S]+)\|([-\s\S]+)\]/-, s)
^
It seems there's something wrong with my syntax (an extra space, missing parentheses, or something like that). Is there something I've missed?
Your code has some special characters which is causing this error:
syntax error, unexpected ')', expecting '='
doc.gs-ub!(/\[([\-s\S]+)\|([-\s\S]+)\]/-, s)
Replace your current code with this piece of code:
class Jekyll::Converters::Markdown::HotelDown < Redcarpet::Render::HTML
#Overriding the preprocess() function
def preprocess(doc)
s = "<ruby><rb>\\1</rb><rp>(</rp><rt>\\2</rt><rp>)</rp></ruby>"
doc.gsub!(/\[([\s\S]+)\|([\s\S]+)\]/, s)
doc
end
end
markdown = Redcarpet::Markdown.new(HotelDown)
and it should work!
I'm attempting to create and use domino to abstract a login page
describe :index, :type => :request do
before do
visit '/'
blah_email_login('user1')
end
...
def blah_email_login(user)
Dom::Email_Link.find_by_name 'Mail'.click
....
end
module Dom
class Email_Link < Domino
selector 'a'
attribute :tab-label
end
here is the html
<a class="tab-label fz-xs accent " href="https://mail.blah.com/..." id="blah"><span class="tab-icon img-sprite"></span><em class="strong tab-link-txt y-link-1 " title="Mail">Mail</em></a>
The process cannot take a dash as indicated my the error I get pre run
C:\blah.rb:93:in `<class:Email_Link>': undefined local variable or method `label' for Dom::Email_Link:Class (NameError)
When I attempted to alter attribute to
attribute :'tab-label'
I got ...
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/domino-0.5.0/lib/domino.rb:114:in `class_eval': (eval):2: syntax error, unexpected '-', expecting ';' or '\n' (SyntaxError)
def tab-label
^
When I included escape characters
attribute :'tab\-label'
I got ...
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/domino-0.5.0/lib/domino.rb:114:in `class_eval': (eval):2: syntax error, unexpected $undefined, expecting ';' or '\n' (SyntaxError)
def tab\-label
^
(eval):4: syntax error, unexpected $undefined, expecting ']'
if value && self.class.callbacks[:tab\-label].is_a?(Proc)
The site I'm working with has many dashed class names, any ideas on how to work with that?
Try:
module Dom
class Email_Link < Domino
selector 'a'
attribute :'tab-label'
end
Update
Domino's default behavior is to replace _ in attribute names to - in class names (see here).
Therefore your attribute name should use underscore:
module Dom
class Email_Link < Domino
selector 'a'
attribute :tab_label
end
When I save the output of a bash command (executed in backticks) to an instance variable, the browser, or more specifically, the variable, removes the "\n" characters. I checked the source code of the page, and it displays like this:
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
But the browser displays it like this:
line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 line 10
I put this on a method that displays the output:
#variable.each_line do |line|
if line !~ /word1| word2/
line.sub("Word_I_don't_want", '')
end
end
But the variable won't be compared to the regular expression, and is printed as a whole. The browser displays a string without <br> as a single line, but the source code shows that new lines are being identified, but not processed. I tried
line.sub("\n", "<br>")
but it didn't work either. Hope someone has some tips for me.
SOLVED:
Thanks to ilan berci I could print the desired values, although the Tin Man pointed out that text should be processed on the controller. So I modified my method:
tempString = String.new
#redes.each_line do |line|
if line !~ /word1|word2/
tempString = tempString + line.sub("word_I_don't_want", "").sub("\n", "<br>")
end
end
#redes = tempString
Because processing the text in the .erb was filling the page code with <br> for every unwanted line (the <br> was casted in every .each_line iteration).
Thanks for the help!
<% #variable.each_line do |line| %>
<% line = if line !~ /word1| word2/ %>
<%line.sub("Word_I_don't_want", '') %>
<% end %>
<%= line %>
<br/>
<% end %>
The problem is how browsers display line-ends and white-space. They "gobble" spaces, tabs, vertical-tabs and line-endings, squeezing them into a single space when displayed.
For instance:
<html>
<body>
foo
bar
</body>
</html>
Will display as foo bar in a browser, even though there is line-end ("\n") between foo and bar.
You can fix that a couple ways:
<html>
<body>
foo<br>
bar<br>
</body>
</html>
Or:
<html>
<body>
<pre>
foo
bar
</pre>
</body>
</html>
would display as:
foo
bar
Whereas:
<html>
<body>
<p>foo</p>
<p>bar</p>
</body>
</html>
would display as:
foo
bar
Browser barely honor whitespace. Words with multiple spaces and line-ends will display as if they only have a single space. If I had a string of characters:
foo\t \n bar
in a page, they would display as:
foo bar
because the browser is trying to be helpful.
How do you "fix" your code? You either wrap your text in a <pre> block, append <br> to the lines where you want line-breaks, or do something fun with CSS to adjust the defaults of <p> or <div> tags. There are lots of ways to get there, you just need to know what you're working with and pick a path.
I've used this in my applications:
def replace_newlines_with_br_tags(text)
h(text).gsub(/\r\n/, '<br />').gsub(/\r/, '<br />').gsub(/\n/, '<br />')
end
You can add this to your application helper and use it in your views, etc.
#variable.gsub(/(?:\n\r?|\r\n?)/, '<br>')
Credits here
Here's my sinatra code:
get '/' do
foo = 'not bar'
erb :index
end
My layout.erb
<html>
<head></head>
<body>
<%= yield %>
</body>
</html>
My index.erb
<div class="container">
</div>
now the problem is
The extra text (hilighted with yellow) disturbs my design
Any idea why this is happening?
this dosn't happen if I dont use layout and use only index.erb with all html code
[Edit:]
Use of <%= yield -%> throws error (unexpected tUMINUS, expecting kEND ; #_out_buf.concat " "; - yield -; #_out_buf.concat "\n" ) in .... layout.rb
my best guess is the 4 spaces come from the soft tabs in your layout.erb
<body>
____<%= yield %>
</body>
try <body><%= yield%></body>?
I've been using Slim a long while
and
body
= yield
never fails me whitespace
hate ERB
You can set this up with *trim_mode* parameter for ERB
From http://ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB.html#method-c-new :
If trim_mode is passed a String containing one or more of the following modifiers, ERB will adjust its code generation as listed:
% enables Ruby code processing for lines beginning with %
<> omit newline for lines starting with <% and ending in %>
> omit newline for lines ending in %>