Slim: Append text to printed Ruby variable - slim-lang

I have the following Slim snippet:
a href = data.api.docs Clicky
What the output is:
Clicky
What I want the output to be:
Clicky
How do I append the #some-subsection to the URL variable in Slim?

Figured it out .. I just had to do:
a href = data.api.docs+"#some-subsection" Clicky
.. to generate:
Clicky

Related

Parser returns wrong url

I'm parsing dialect words from http://www.dialettando.com/dizionario/hitlist_regioni_new.lasso?regione=Sardegna.
from urllib import request
from bs4 import BeautifulSoup
from nltk import corpus, word_tokenize, FreqDist, ConditionalFreqDist
url = 'http://www.dialettando.com/dizionario/hitlist_regioni_new.lasso?regione=Sardegna'
dialettando_tokens = []
while url:
html = request.urlopen(url).read().decode('utf8')
page = BeautifulSoup(html, 'html.parser')
a_list = page.find_all('a')
for a in a_list:
try:
a_str = str(a.contents[0])
if a_str[:3] == '<b>' and a.contents[0].string:
dialettando_tokens.append(a.contents[0].string.strip())
except:
pass
if a.string == 'Simonelli Editore Srl':
break
elif a.string == 'PROSSIMI':
link = a['href']
url = 'http://www.dialettando.com/dizionario/' + link
break
else:
url = ''
In the end of each iteration I need to parse url to the next page.
HTML:
PROSSIMI
And I need to get this link:
'hitlist_regioni_new.lasso?saltarec=20&ordina=parola_dialetto&regione=Sardegna'
BUT the parser returns:
'hitlist_regioni_new.lasso?saltarec=20&ordina=parola_dialettoRione=Sardegna'
This link doesn't work correctly and I can't understand what's wrong.
An href needs to have the ampersand character escaped, see this question. It is possible the site you visited is not escaping the ampersand inside the href correctly, and hoping they never accidentally reference an HTML entity, except in your case they did. It seems like you have to parse buggy HTML, plus a parser that didn't notice the semicolon was missing and did the HTML entity conversion anyway.

How to concatenate two strings in image source with razor?

I've defined the following variable in razor:
#{string imageRoot = "/_media/Images/";}
I'd like to use it here:
<img src="#imageRoot App1/MyImage.png"/>
The problem is the space within the string.
This will work but I'd like to keep the trailing slash in the variable instead of in the literal:
#{string imageRoot = "/_media/Images";}
<img src="#imageRoot/App1/MyImage.png"/>
Looks a little ugly, but works:
#{string imageRoot = "/_media/Images/";}
<img src="#Html.Raw(imageRoot)App1/MyImage.png" />
Use String.Concat to merge the two string.
#{imgRoot = string.Concat(imgRoot,"/_media/Images");}
or just normal imgRoot + = imgRoot + "/_media/Images"
then set your variable as <img src='#imageRoot'/>
or <img src="#{imageRoot+#"/App1/MyImage.png"}"/>
Note that the #-symbol is a Verbatim string literal and will prevent the \ from being escaped should you have a character that escapes your string

Rails assets: Using a js/cofee variable for specifying name of image

I'm new to rails and I'm still a little confused about the rails assets pipeline.
I have an array with a bunch of strings and each string has an image associated with it. I want the src of the image to equal the string + '.png'.
Here's some code that I tried to write (which doesn't work). I understand that the reason this doesn't work is because the ruby code is executed when compilation happens, but I'm not sure how else to do it. Thanks for your help!
for flavor in flavors
curFlavorImage = document.createElement('img')
$(curFlavorImage).attr('src', "<%= asset_path('" + flavor['flavor'] + '.png' + "') %>")
flavor is a javascript variable, it can't be used by the ruby/erb code.
If you have a fixed number of images, I would do like this:
var flavor_images = [];
flavor_images['flavor1'] = '<%= asset_path("flavor1.png") %>';
flavor_images['flavor2'] = '<%= asset_path("flavor2.png") %>';
// ...
for flavor in flavors
curFlavorImage = document.createElement('img')
$(curFlavorImage).attr('src', flavor_images[flavor['flavor']]);

How to add file extension inside of url with ruby

This url:
url = rtmp://xxxxxxxxxxxxxx.cloudfront.net/cfx/st/mp3:audios/lesson/2/cancion?Expires=1386332537&Signature=mysignature__&Key-Pair-Id=my-key-par-id
I would like to add the extension mp3 to all file name.
In this case the file name is cancion
The id of lesson is a dynamic value.
I would like to get this url something like:
url = rtmp://xxxxxxxxxxxxxx.cloudfront.net/cfx/st/mp3:audios/lesson/2/cancion.mp3?Expires=1386332537&Signature=mysignature__&Key-Pair-Id=my-key-par-id
Thanks!
You can parse the URI, edit the path, then return the value
require 'uri/http'
u = URI.parse('rtmp://xxxxxxxxxxxxxx.cloudfront.net/cfx/st/mp3:audios/lesson/2/cancion?Expires=1386332537&Signature=mysignature__&Key-Pair-Id=my-key-par-id')
u.path += ".mp3"
puts u.to_s
or use a simple regexp replace
u = 'rtmp://xxxxxxxxxxxxxx.cloudfront.net/cfx/st/mp3:audios/lesson/2/cancion?Expires=1386332537&Signature=mysignature__&Key-Pair-Id=my-key-par-id'
u.gsub('?', '.mp3?')
The second approach can be used only if you can assume the format of the input is always the same.
You can do simple gsub since this is URL and you can expect one occurrence of ? so simple do.
url.gsub!('?', '.mp3?')
Usually I would go regex here but no need from previously stated reason.

URL manipulation: http://example.com/foo.jpg -> http://example.com/foo.preview.png

In rails I want to wrote some code to change this url string
https://img.skitch.com/20101222-kg5chjx4jetgcdeaug46hi6jpk.jpg
to
https://img.skitch.com/20101222-kg5chjx4jetgcdeaug46hi6jpk.preview.png
Should I use regular Expression to change it?
I'm new to Regexp, anyone can show me how to do this, and how to learn this stuff
thanks
If the extension is of fixed length, you're better off using string slicing.
url = "https://img.skitch.com/20101222-kg5chjx4jetgcdeaug46hi6jpk.jpg"
print url[0..-5] + ".preview" + url[-4..-1]
outputs
https://img.skitch.com/20101222-kg5chjx4jetgcdeaug46hi6jpk.preview.jpg
Or if your extensions are of variable length you can use rindex() to find the start of the extension.
url = "https://img.skitch.com/20101222-kg5chjx4jetgcdeaug46hi6jpk.jpeg"
dot_index = url.rindex(".")-1
print url[0..dot_index] + ".preview" + url[dot_index+1..-1]
outputs
https://img.skitch.com/20101222-kg5chjx4jetgcdeaug46hi6jpk.preview.jpeg
If you must use a regex then do it like this:
url = "https://img.skitch.com/20101222-kg5chjx4jetgcdeaug46hi6jpk.jpeg"
print url.gsub(/\.(\w{2,4})$/, ".preview.\\1")
outputs
https://img.skitch.com/20101222-kg5chjx4jetgcdeaug46hi6jpk.preview.jpeg
If you're sure the file ends with .jpg, you can to
url = "https://img.skitch.com/20101222-kg5chjx4jetgcdeaug46hi6jpk.jpg"
url.gsub(".jpg", ".preview.jpg")
Otherwise, you can get the filename, then append the extension.
url = "https://img.skitch.com/20101222-kg5chjx4jetgcdeaug46hi6jpk.jpg"
ext = File.extname(url)
url.gsub(ext, ".preview{ext}")
A string replace seems to be enough.
".jpg" -> ".preview.png"
Unfortunately I do not know ruby.
In python it'll be
new_url = url.replace(".jpg",".preview.png",1)
I think that it'll be similar in ruby. It seems to be sub() instead.
new_url = url.sub(".jpg",".preview.png")

Resources