HAML how to craft this line? - haml

I got this code
%p.date= "Submitted #{time_ago_in_words(#post.created_at)} ago | " |
= link_to "Edit Post", edit_post_path(#post)
I get unexpected result:
<p class="date">Submitted 10 minutes ago | </p>
Edit Post
I want to get a tag inside p tag:
<p class="date">Submitted 10 minutes ago | Edit Post</p>
I have also tried this:
%p.date= "Submitted #{time_ago_in_words(#post.created_at)} ago | "
= link_to "Edit Post", edit_post_path(#post)
and this:
%p.date= "Submitted #{time_ago_in_words(#post.created_at)} ago "
= "| #{link_to "Edit Post", edit_post_path(#post)}"
In both cases I get same error
Illegal nesting: content can't be both given on the same line as %p and nested within it.
It seems that haml isn't aware that ending | is withing ruby string?
How to fix this?

Break them in next line with indentation to nest them:
%p.date
= "Submitted #{time_ago_in_words(#post.created_at)} ago | "
= link_to "Edit Post", edit_post_path(#post)

Related

How to get all text from HTML code using awk or grep or sed?

Here is an example of html code I have in a file:
+ "name": "Product 1",
- <title>Cool</title>
+ Nice music
- Great music
- <a class="button" href="/url" data-name="buy">Buy <span class="product">Product 1</span></a>
+ <a class="button" href="/index" data-name="buy">Buy <span class="product">Product 1</span></a>
- Product (1st generation)
+ Product <br class="break" />(1st generation)
- Cool
+ Not cool
- <li> Nice > cool > good. ok.</li>
+ <li> Nice > cool > good. Tap.</li>
+<meta property="track" content="page_1" />
+<meta property="track" content="page_2" />
+ "name": "Product 2",
Expected output:
+ "name": "Product 1",
- Cool
+ Nice music
- Great music
- Buy Product 1
+ Buy Product 1
- Product (1st generation)
+ Product (1st generation)
- Cool
+ Not cool
- Nice > cool > good. ok.
+ Nice > cool > good. Tap.
+ "name": "Product 2",
Here is my code
awk 'BEGIN{RS="<";FS=">"}NF>1{printf "%s", $NF ""}'
I get following result which is not expected:
Cool
+ Nice music
- Great music
- Buy Product 1
+ Buy Product 1
- Product (1st generation)
+ Product (1st generation)
- Cool
+ Not cool
- good. ok.
+ good. Tap.
+
+
+ "name": "Product 2",
How to get expected result using grep or awk or sed? It should be able to get all text file from a html page.
This might work for you (GNU sed):
sed 's/<[^<>]*>//g;/^[+-]\s*$/d' file
Remove all tags.
Remove empty lines.
I would harness GNU AWK for this task following way, let file.txt content be
+ "name": "Product 1",
- <title>Cool</title>
+ Nice music
- Great music
- <a class="button" href="/url" data-name="buy">Buy <span class="product">Product 1</span></a>
+ <a class="button" href="/index" data-name="buy">Buy <span class="product">Product 1</span></a>
- Product (1st generation)
+ Product <br class="break" />(1st generation)
- Cool
+ Not cool
- <li> Nice > cool > good. ok.</li>
+ <li> Nice > cool > good. Tap.</li>
+<meta property="track" content="page_1" />
+<meta property="track" content="page_2" />
+ "name": "Product 2",
then
awk 'BEGIN{FS="<[^>]*>";OFS=""}{$1=$1}/.../' file.txt
gives output
+ "name": "Product 1",
- Cool
+ Nice music
- Great music
- Buy Product 1
+ Buy Product 1
- Product (1st generation)
+ Product (1st generation)
- Cool
+ Not cool
- Nice > cool > good. ok.
+ Nice > cool > good. Tap.
+ "name": "Product 2",
Explanation: I inform GNU AWK that field separator (FS) is < followed by zero or more (*) not (^) > followed by > and output field separator (OFS) is empty string. For each line I do $1=$1 to trigger string rebuilt. Then I select line which matches ... expression, that is only lines having at least 3 characters. Disclaimer: HTML is not Chomsky Type 3 contraption and therefore can not be robustly parsed using regular expression, proposed code use certain heuristic which hopefully will work well enough with data you want to process.
(tested in gawk 4.2.1)

Scrape text under div tag that is in quotes

Trying to scrape this part: "Lounge, Showers, Lockers"
https://i.stack.imgur.com/k5mzg.png
<div class="CourseAbout-otherFacilities more">
<h3 class="CourseAbout-otherFacilities-title">Available Facilities</h3> " Lounge, Showers, Lockers "
</div>
Website:
https://www.golfadvisor.com/courses/16929-black-at-bethpage-state-park-golf-course
response.css('.CourseAbout-foodAndBeverage.more::text').get() command returns " \n "
Thank you
There are three text elements in your target div (matched by your CSS expression):
<div class="CourseAbout-otherFacilities more">FIRST<h3
<h3 class="CourseAbout-otherFacilities-title">SECOND</h3>
</h3>THIRD</div>
By using .get() you're telling Scrapy to return first match.
I recommend to use XPath expression here instead and match your element by text:
//h3[.="Available Facilities"]/following-sibling::text()[1]'

Robot Framework for loop - selector always returning same element

I have an unordered list, from which I need to get the title from each element.
li class="chart-flyout-option" ng-repeat="option in filteredOptions = ($ctrl.options | filter: { value: $ctrl.filterText })" ng-class="{'chart-flyout-option-selected': option.key == $ctrl.selectedOption.key}"><a class="chart-flyout-link" ng-bind-html="option.value | highlight:$ctrl.filterText" title="HERE IS TITLE 1" ng-click="$ctrl.select(option.key, $event)" data-key="126">HERE IS TITLE 1</a></li>
Next element would be similar, but assume - HERE IS TITLE 2
I have a for loop, which looks like this:
Get Flyout Entry Child Links
${numflyoutentries}= Get Number Chart Flyout Entries
${numflyoutentries}= Evaluate ${numflyoutentries} + ${1}
: FOR ${entry} IN RANGE 1 ${numflyoutentries}
\ ${label}= Get Text class:chart-flyout-link
\ Log ${label}
However this only ever returns the text "HERE IS TITLE 1".
Why doesn't my selector increment?

Select and "ol" html tag with Watir Webdriver

I want to create a for loop to get the text from several lis with watir.
Here's is the HTML I'm trying to scrape
<ol class="shared-connections">
<li class="small-result">
<a class="img-link" href="http://www.google.com"></a>
</li>
<li class="small-result">
<a class="img-link" href="http://www.google.com"></a>
</li>
</ol>
I'm tring to get the href value in the links with a loop, but I can't get the loop to initiate with this code:
#browser.ol(class: "shared-connections").lis(class: "small-result").each do |connection|
p "is this working?"
end
The "ol" tag prevents the loop from working and gives me this error:
/Library/Ruby/Gems/2.0.0/gems/watir-webdriver-0.9.1/lib/watir-webdriver/elements/element.rb:536:in `assert_element_found': unable to locate element, using {:class=>"shared-connections", :tag_name=>"ol"} (Watir::Exception::UnknownObjectException)
Any idea how to get "ol" to work with Watir? Thanks!
It seems you didn't open proper page via code.
Code is working well. I created file with code you provided
in irb:
pp File.readlines('a.html')
["<ol class=\"shared-connections\">",
" <li class=\"small-result\">,
" <a class=\"img-link\" href=\"http://www.google.com\"></a>,
" </li>",
" <li class=\"small-result\">,
" <a class=\"img-link\" href=\"http://www.google.com\"></a>",
" </li>",
"</ol>"]
Then
b = Watir::Browser.new :chrome
b.goto 'file://' + Dir.pwd + '/a.html'
b.ol(class: "shared-connections").lis(class: "small-result").each do |connection|
p "is this working?"
end
"is this working?"
"is this working?"
=> [#<Watir::LI:0x604055b6f2097db6 located=false selector={element: (webdriver element)}>, #<Watir::LI:0x..f5826cdc74313e1a located=false selector={element: (webdriver element)}>]
You can ensure about this with #browser.html

Selecting items IN a row

I'm developing a website for Tenants to find properties. When they sign up, they can choose the property types that they are interested, for example: Apartment or House.
When a Tenant logs into their account, they can then do a search for properties. The search form is prepopulated with the values that they originally entered on sign up, for example: City, Postcode and so on.
The form also needs to display some checkboxes with the relevant boxes ticked for the Property Types that they selected on sign up. I'm having some problems getting this to work and wondered if there is anyone who could correct the code for me?
I believe I need to use an 'IN' statement so that the relevant checkboxes would be ticked, if the IDs for those properties are found in the CustomerReqPropertyType column. The CustomerReqPropertyType column is varchar(50) and as an example, if a user has selected Apartment and House, it is store in the row as 2, 4 (as there is a separate table with the Property Types.
This is the code I have on the page;
<%
While (NOT rspropertytype.EOF)
%>
<li>
<input type="checkbox" name="txtPropertyType" id="txtPropertyType" value="<%=(rspropertytype.Fields.Item("PropertyTypeID").Value)%>"<% If Not rstenantrequirements.EOF Or Not rstenantrequirements.BOF Then %><%If (Not isNull((rstenantrequirements.Fields.Item("CustomerReqPropertyType").Value))) Then If (CStr(rspropertytype.Fields.Item("PropertyTypeID").Value) = CStr((rstenantrequirements.Fields.Item("CustomerReqPropertyType").Value))) Then Response.Write("")%><% End If ' end Not rstenantrequirements.EOF Or NOT rstenantrequirements.BOF %> />
<label for="txtPropertyType"><%=(rspropertytype.Fields.Item("PropertyTypeTitle").Value)%></label>
</li>
<%
rspropertytype.MoveNext()
Wend
If (rspropertytype.CursorType > 0) Then
rspropertytype.MoveFirst
Else
rspropertytype.Requery
End If
%>
I would be very grateful for any help.
In order for a checkbox to be checked the checked property must equal "checked".
e.g.
<input type="checkbox" name="something" value="somethingElse" checked="checked" />
I suspect that in your code the rspropertytype and rstenantrequirements recordsets could be consolidated into one recordset generated from one SQL statement.
e.g.
SELECT pt.*
, CASE
WHEN ISNULL(tr.CustomerReqPropertyType,0) = 0 THEN 0
ELSE 1 END AS [checked]
FROM propertytype AS [pt]
LEFT JOIN tenantrequirements AS [tr]
ON pt.PropertyTypeID = tr.CustomerReqPropertyType
WHERE ...
Then your ASP code could be simplified as well.
e.g.
<%
While (NOT rs.EOF)
Dim pID : pID = rs("PropertyTypeID")
Dim pTitle : pTitle = rs("PropertyTypeTitle")
Dim checked : checked = "" : If (rs("checked") = 1) Then checked = "checked"
%>
<li>
<input type="checkbox" name="txtPropertyType" id="txtPropertyType<%=pID%>" value="<%=pID%>" checked="<%=checked%>" />
<label for="txtPropertyType<%=pID%>"><%=pTitle%></label>
</li>
<%
rs.MoveNext()
Wend
%>
This is really hard to follow. First I'd break up that line to where your while is you can just set a variable and print that to page. I assume you're trying to set the checkbox checked. What I would do is make sure the value returned isn't null assuming this is a query that just returns the property type and you left joined it with the table that has the descriptions in it when they are set for the property in question. I don't see you ever print checked to the checkbox so it's never going to be checked anyway.