0

This is continuation from This one. Read following question&comments first before looking at this one.

I have following Replacement(Hash) below for switching text on html that I've got from desired url of website using open-uri library.

replacements = {'1杯の'=>'a cup of','たくさん'=>'a lot','たくさんの'=>'a lot of','1週間'=>'a week','午前'=>'a.m.','およそ'=>'about','住所'=>'adress','大人'=>'adult','アフリカ'=>'Africa',‌​'の後で'=>'after','のあとで'=>'after','午後'=>'afternoon','再び'=>'again','ふたたび'=>'again','空‌​港'=>'airport','全て'=>'all','一日中'=>'all day','1日中'=>'all day','いつも'=>'always'...}

And I want to add <a> tag to all english word at hash above, which is linked to online Japanese-English Dictionary service called "Weblio"

Weblio's url format is like this: http://ejje.weblio.jp/{desired english word}

so that <a>tag which should be added to all english word should be like below.

<a href="http://ejje.weblio.jp/{desired english word}">DESIRED+ENGLISH+WORD</a>

Any way to do the easily? I just started learning Ruby on Rails recently, so it would be nice to have detailed and simple explanation for it with some examples.

EDIT

def submit
    require 'uri'
    require 'open-uri'
    require 'nokogiri'

    charset = nil
    @html = open(params[:url]) do |f|
    charset = f.charset
    f.read
    end

    replacements = {'1杯の'=>'a cup of','たくさん'=>'a lot'...}
    regex = Regexp.new(replacements.keys.map { |x| Regexp.escape(x) }.join('|'))
    @html = Nokogiri::HTML::DocumentFragment.parse(@html)
    @html.traverse do |x|
    if x.text?
        x.content = x.content.gsub(regex, replacements)
    end
    end
end

EDIT2:

I have a problem, which all <a> tag replaced actually displays as a part of <p> tag and not linking. How I can escape <a> tag form <p>?

EDIT3:

This is what actually happening. https://techacademy-leoreo2247.c9users.io/result/?utf8=%E2%9C%93&url=https%3A%2F%2Fringo-12.com%2F inspect element on <a href="http://..../content/windows">windows</a>for more details

EDIT4:

Seems like it has different problem. When I inspected element on my app, the code it should display likeWindowsis displaying like &lt;a href="http://ejje.weblio.jp/content/Windows"&gt;Windows&lt;/a&gt;.

I guess the code "Windows"is encoded to some other format.

Community
  • 1
  • 1
LEoREo_2247
  • 100
  • 9

1 Answers1

2

Weblio's url format is like this: http://ejje.weblio.jp/{desired english word}

Well, not quite: you can't have spaces in a url, so you have to do something extra to a phrase like "a lot of".

In rails, by default you use something called ERB to substitute values into a view. ERB allows you to write ruby code in your html and execute it. All your view files have the extension .html.erb--that is a hint that rails first runs your views through the ERB templating engine to produce the .html files. So, you need to read a tutorial on ERB syntax, such as this one. Here is an example of what ERB looks like:

<% 
  replacements = {
      '1杯の'=>'a cup of',
      'たくさん'=>'a lot',
      'たくさんの'=>'a lot of',
  }

  replacements.values.each do |phrase| %>
    <a href="http://ejje.weblio.jp/<%= URI.escape(phrase) %>"><%= phrase %></a>
  <% end %>

A <%= .... %> block inserts the results of the ruby code contained in the block into the html.
A <% ..... %> block is for ruby code that you want to be executed but whose results are not to be inserted into the html.

If you create your replacements in an action, e.g.:

def dostuff
  @replacements = {
    ...
  }
end

then your view would simply look like this:

  <% @replacements.values.each do |phrase| %>
    <a href="http://ejje.weblio.jp/<%= URI.escape(phrase) %>"><%= phrase %></a>
  <% end %>

Below is an example that demonstrates the use of the ERB templating engine outside of rails.

require 'erb'
require 'uri'

template = %q{ #=> Another way in ruby to write an opening single quote for a string.
<% 
  replacements = {
    '1杯の'=>'a cup of',
    'たくさん'=>'a lot',
    'たくさんの'=>'a lot of',
  }

  replacements.values.each do |phrase| %>
    <a href="http://ejje.weblio.jp/<%= URI.escape(phrase) %>"><%= phrase %></a>
  <% end %>
}  #=>Closing single quote for the string

renderer = ERB.new(template)
result = renderer.result()
puts result

--output:--
<a href="http://ejje.weblio.jp/a cup of">a cup of</a>

<a href="http://ejje.weblio.jp/a lot">a lot</a>

<a href="http://ejje.weblio.jp/a lot of">a lot of</a>

In rails, you don't need to write the require or ERB.new() or renderer.result()--rails does all that automatically for an .html.erb file

Once you write a few views using ERB, and you feel comfortable with the basics, you can promptly forget about ERB and use one of the better templating languages, like slim, which is much prettier and easier to type than ERB. Rails has the means for incorporating any templating engine of your choice--but the default is ERB.

Response to comment:

 def dostuff

    replacements = {
      '1杯の'=>'a cup of',
      'たくさん'=>'a lot',
      'たくさんの'=>'a lot of',
    }

    replacements.each do |key, val|
      replacements[key] = %Q{<a href="http://ejje.weblio.jp/#{URI.escape(val)}">#{val}</a>}
    end

    require 'pp'  #pretty print
    pp replacements

  end

Output in server window:

{"1杯の"=>"<a href=\"http://ejje.weblio.jp/a%20cup%20of\">a cup of</a>",  
 "たくさん"=>"<a href=\"http://ejje.weblio.jp/a%20lot\">a lot</a>",  
 "たくさんの"=>"<a href=\"http://ejje.weblio.jp/a%20lot%20of\">a lot of</a>"}

Response to comment:

Your str is html escaped. If you need to literally display all the characters in the string <a>hello</a> on a web page, you must replace the angle brackets with what are called html entities, otherwise the browser will render the <a> tag as a link. The html entity for the < character is &lt;, where lt stands for less than, as in a less than symbol in a mathematical comparison: if x < 4. Similarly, the html entity for the > character is &gt;, as in greater than.

require 'cgi'

str = %q{<a href="http://ejje.weblio.jp/content/Windows">Windows</a>}

puts "<div>This is an html instruction site.  First we will start with link tags.</div>"
puts "<div>Link tags look like this:</div>"
puts "<div>#{CGI.escapeHTML str}</div>"

str = %q{&lt;a href="http://ejje.weblio.jp/content/Windows"&gt;Windows&lt;/a&gt;}
html = CGI.unescapeHTML str

puts "<div>Here is what that link looks like when it is rendered by a browser:</div>"
puts "<div>#{html}</div>"


--output:--
<div>This is an html instruction site.  First we will start with link tags.</div>
<div>Link tags look like this:</div>
<div>&lt;a href=&quot;http://ejje.weblio.jp/content/Windows&quot;&gt;Windows&lt;/a&gt;</div>
<div>Here is what that link looks like when it is rendered by a browser:</div>
<div><a href="http://ejje.weblio.jp/content/Windows">Windows</a></div>

If you take the output of that program and open it in a browser, you will see:

This is an html instruction site. First we will start with link tags.
Link tags look like this:
<a href="http://ejje.weblio.jp/content/Windows">Windows</a>
Here is what that link looks like when it is rendered by a browser:
Windows

7stud
  • 46,922
  • 14
  • 101
  • 127
  • thank you for your answer. I want to do that in action file, but not very sure about hot to do it. I have added my action file in my question, Please give me some advice for adding 'replacements.values.each do' method in action file. – LEoREo_2247 Jun 13 '16 at 08:02
  • @LEoREo_2247, First, what is the result you want? An array of strings? – 7stud Jun 13 '16 at 08:32
  • So, I am making app that shows desired url of webpage with replacement of japanese word to english word. now, app can just get html from desired url of webpage, and show the page inline. now, i want to replace each japanese word to english word with link to weblio dictionary(Its not like Google Translate, its just an app that can improve vocabulary.) – LEoREo_2247 Jun 13 '16 at 08:37
  • Thank you so much..! this is exactly the one that I was looking for. I will look at code and try few more patterns later ;) – LEoREo_2247 Jun 13 '16 at 10:40
  • I Edited Question, please check it. – LEoREo_2247 Jun 14 '16 at 02:32
  • html is the one i got it from website direndtly, so i cant move it i guess – LEoREo_2247 Jun 14 '16 at 06:12
  • I figured out reason. But not sure how to fix it. I have edited my question again, so please check it – LEoREo_2247 Jun 17 '16 at 16:37
  • @LEoREo_2247, See response at bottom of my answer. – 7stud Jun 17 '16 at 19:45
  • Thanks for reply. 1.replacements[key] looks like this now. `replacements[key] = %Q{<a href="http://ejje.weblio.jp/content/#{URI.escape(val)}">#{val}</a>}` 2.And escaped entire html with code that you mentioned but it does not escape html. 3.but if I remove all assigns and replace it like this`replacements[key] = %Q{<a href="http://ejje.weblio.jp/content/Windows">Windows</a>}` somehow it works. – LEoREo_2247 Jun 18 '16 at 03:08
  • @LEoREo_2247, *And escaped entire html with code that you mentioned*--It sounds like you did things backwards. I escaped the html to get a string like you have--then I **un**escaped that result to get the html back--that is what I thought you wanted to do. You need to unescape your string: `<a href="http://ejje.weblio.jp/content/Windows">Windows</a>` to get html. See the unescape lines in my example. – 7stud Jun 18 '16 at 04:34