Ruby Haml - cuts out tag (&& ||) attributes - ruby

Strange behaviour of haml - it cuts out tag attributes.
for example, i write two ways:
first - head inside layout:
!!!
%html{ lang: I18n.locale }
%head{ 'data-hook' => 'inside_head' }
%title= "sample title"
%meta{ content: 'text/html; charset=UTF-8', 'http-equiv' => 'Content-Type' }
it produce next code:
<!DOCTYPE html>
<html lang="ru">
<head data-hook="inside_head">
<title> sample title
</title>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
if not look on identation, all is fine, but if i write head in partial and render it, haml cuts out head tag, but passing content of partial!
code, my second and preffered way is:
!!!
%html{ lang: I18n.locale }
= render 'shared/head', title: "sample app"
and partial in shared/head.haml:
%head{ 'data-hook' => 'inside_head' }
%title= title
%meta{ content: 'text/html; charset=UTF-8', 'http-equiv' => 'Content-Type' }
but, haml produce next strange code, tag 'head' is missed:
<!DOCTYPE html>
<html lang="ru">
<body>
<title> sample app
</title>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
What i doing wrong? Or Haml is buggy?
See my layout file:
See my head file:
See result html:

Try to rename _head.haml to _head.html.haml. Works for me.
So the final usage will be the same:
!!!
%html
= render 'shared/head'
And btw haml has better way to pass attributes for haml tags:
%head(data-hook='inside_head')

Related

How to set and change ( title of page ) in inertiajs and vue3

I'm using vue3 and inertiaJs with Laravel8
I want to change the (TITLE of page) if I change the page
I checked in Inertia documentation this code but it doesn't work with me and the title of the page doesn't change
I installed vue-meta
metaInfo() {
return() {
title: “Home Page”,
}
}
return() is a property, not a function. :) So you should do it like this: return
// parent
metaInfo: {
meta: [{
vmid: 'description',
name: 'description',
content: 'my standard description',
}]
}
// child
metaInfo() {
return {
meta: [{
vmid: 'description',
name: 'description',
content: this.description,
}]
}
},
Learn more at this Vue doc
In "inertia-vue3", how do we know that "description" has changed?
import { Head } from '#inertiajs/inertia-vue3'
<Head>
<title>About - My app</title>
<meta head-key="description" name="description" content="This is a page specific description" />
</Head>
In the view-source, nothing can be understood !
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title inertia>test</title>
<body>
</body>
</html>
As per v0.4.0 of Inertia, they now bundle their own inertia-head component you can use to add meta tags and update the title. There is no longer a need for vue-meta.
Simply add the following directly to your page components:
<inertia-head>
<title>Your page title</title>
<meta name="description" content="Your page description">
</inertia-head>
You can find more information in the change logs of v0.4.0.

Metalsmith doesn't escape output of Markdown + Nunjucks

I'm stuck with this problem. When I use Markdown + Nunjucks as explained in the metalsmith-in-place Wiki the output is wrong (see below).
The default layout, note the safe filter (_layouts/base.njk):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{{title}}</title>
</head>
<body>
{{ contents | safe }}
</body>
The template that is using Markdown + Nunjucks (about.md.njk):
---
title: About
layout: base.njk
---
# {{ title }}
Output:
<p><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>About</title>
</head>
<body></p>
<h1>About</h1>
<p> </body>
</html></p>
Solved right after posting the question. This may be help, the problem is the order of plugins in my build.js:
Metalsmith(__dirname)
.source('./contents')
.destination('./build')
.clean(true)
.use(inPlace()) // inPlace must come BEFORE layouts!
.use(layouts({
directory: '_layouts',
default: 'base.njk'
}))
.build(function(err) {
if (err) throw err;
});

How to add comment into an XML file

How do I add a comment into an XML file using Nokogiri?
For example, I have an existing html file. I want to add <!--doc-->. How should I do it so I get:
...
<body>
<!--doc-->
</body>
...
I'd use:
require 'nokogiri'
doc = Nokogiri::HTML('<html><body></body></html>')
doc.at('body') << '<!-- foo -->'
puts doc.to_html
# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html><body><!-- foo --></body></html>
Or you can use a bit longer code:
doc.at('body').add_child('<!-- foo -->')
Which results in the same thing.
It gets a little more interesting/complicated if <body> has more nodes, and you care where the comment goes, but that's still basically locating where you want the comment to be inserted, and then doing one of the above.
I use following code fix:
require 'nokogiri'
d = Nokogiri::HTML(%Q(<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
</body>
</html>
))
d.css('body')[0].add_child(Nokogiri::XML::Comment.new(d, "doc"))
puts d.to_s

XPATH - add concatenation into multiple attributes

Here is my code:
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns:fb="http://www.facebook.com/2008/fbml" xmlns:og="http://opengraphprotocol.org/schema/">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<link rel="alternate" hreflang="en" href="http://www.example.com/page-59.html"/>
<div id="" class="pgLinks">
«
1
<span class="paging pageDisplay">2</span>
When I run this query, it returns either the top URL on the page "http://www.example.com/page-59.html" OR if it a "1" is present here:
1
it returns the URL from the href which is:
/example-text-2
The thing is I want the full URL:
http://www.example.com/example-text-2
I basically need to add a URL to the second part of this so it joins the second result if present, so it is something like this:
(//link[#hreflang='en'] | "SITE URL HERE" //div[#class='pgLinks']/a[.='1'])[last()]/#href
I have tried concat:
(//link[#hreflang='en'] | concat("http://www.example.com", //div[#class='pgLinks']/a[.='1']))[last()]/#href)
And so many other variations including using the pipe "|" but cannot figure it out at all.
Grateful for any help.
Assuming you only have xpath 1.0 support, you can do the xpath:
concat(
substring(
concat(
'http://www.example.com',
//div[#class='pgLinks']/a[.='1']/#href
),
1 div boolean(//div[#class='pgLinks']/a[.='1'])
),
substring(
//link[#hreflang='en']/#href,
1 div not(//div[#class='pgLinks']/a[.='1'])
)
)
This is an application of an answer on implementing an if-else statement.

Nokogiri -- preserve doctype and meta tags

I'm using nokogiri to open an existing html file that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Foo</title>
</head>
<body>
<!-- stuff -->
</body>
</html>
Then I change the contents of the body tag like this:
html_file = Nokogiri::HTML("path/to/html/file")
html_file.css('body').first.inner_html = "new body content"
Then I write this new document to a file like this:
File.open("path/to/new/html/file", 'w') {|f| f.write html_file}
And this is my resulting html file:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
new body content
</body></html>
My question for you guys if it's possible to tell Nokogiri to preserve the original html file's doctype and meta tags, since it appears like they are being lost/changed when I open the document with Nokogiri and attempt to write it to a file.
Any help would be much appreciated. Thanks!
Finally figured it out:
I just changed the line:
html_file = Nokogiri::HTML("path/to/html/file")
to
html_file = Nokogiri::HTML(File.open("path/to/html/file").read)
and now it works like I'm expecting it to. Seems kind of inconsistent, but I'm sure there's a good reason for it.
Thanks for all of the suggestions #ezkl!

Resources