Using the Sinatra library, I'm trying to condense two functions that display HTML code into a single function. Both these functions differ by only a small amount of HTML.
Here's an example.
def make_start_page()
<<EOS
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<p> Hello </p>
<img src="..." />
</body>
</html>
EOS
end
def make_guess_page()
<<EOS
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<p> Something different </p>
<a href="..." >1</a>
</body>
</html>
EOS
end
In the Ruby function that will call these two functions, I was wondering if it is possible to take the small portion of HTML that differs and pass it to a single, condensed version of these two functions that will display the page.
def handle()
if 1
var = "<p> Hello </p>
<img src="..." />"
elsif 2
var = "<p> Something different </p>
<a href="..." >1</a>"
make_start_guess_page(var)
end
You can interpolate variables in heredoc:
def make_start_page(var)
<<EOS
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
#{var}
</body>
</html>
EOS
end
For example.
There no reason why you could not do that. However if you want to print it, you'll probably have to use functions like String#html_safe in rails, or != in haml
Related
I would like to have dynamic value in my html file which gets Convertet to a PDF file. I've got no idea how to do that.
HTML
<!doctype html>
<html lang="en">
<head>
<title>SpringHow html to pdf</title>
</head>
<body>
<div>
<h2> --> DYNAMIC VALUE HERE <-- </h2>
</div>
</body>
</html>
JAVA
HtmlConverter.convertToPdf(new File("template.html"),new File("report.pdf"));
I am trying to use an HTML template as given below and set 2 values to it dynamically.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title th:text="${title}"></title>
</head>
<body>
<div class="page-content">
<h1 th:text="|Hello ${siteCode}!|"></h1>
</div>
</body>
</html>
I used the below code to set the values.
Model model = new ExtendedModelMap();
model.addAttribute("title", site.getName());
model.addAttribute("siteCode", site.getCode());
I want to get the new html file with the values added in the template variable so that I can store the file to s3. Please help.. Thanks in advance.
I have a server at batalabs.tk where I use batch files as most of my CGI scripts, I am currently trying to make an Indexing service for a search script...
Example files:
Directory to search:
<DIR HTDOCS>
index.html
otherpage1.html
otherpage2.html
</DIR>
index.html:
<HTML>
<HEAD>
<TITLE>MyWebsite - Buy Products</TITLE>
<META NAME="Description" Content="Buy Software and products from MyWebsite">
</HEAD>
<BODY>
<DIV ALIGN="CENTER">
<H2>Software</H2>
<h4>Find the latest and greatest in software right here at the Shop Page!</h4>
<hr>
Lorum - Free Cloud Storage
<hr>
Ipsum - Free Networking Software
<hr>
</div>
Copyright MyWebsite.com, 2016
</body>
</html>
Otherpage1.html
<html>
<head>
<title>Page One - Hello World!</title>
<META Name="Description" Content="Hello World Page For Testing.">
</head>
<body>
<h1>
Hello, World!
</h1>
<div>
<pre>
this is an example page for use with testing.
Any of these words can be searched and should (optimally) show this page in the results.
</pre>
</div>
</body>
</html>
otherpage2.html:
<html>
<head>
<title>Page Two - The Revenge Of The Coder</title>
<meta Name="Tags" content="dummy; coder; file; otherpage2;;">
</head>
<body>
<pre>
Hello, This file doesn't matter. At all. It is a dummy.
</pre>
</body>
</html>
These are just example files, But i want my batch file to pick up on the
<META> tag with Description and Tags, as well as the file's name, every word in the body,
and the title... I've tried some stuff to no avail, any help?
So maybe a user could type "dummy" as their keyword, and Otherpage2.html would pop up like so:
Page Two - The Revenge Of The Coder
HTTPS://www.batalabs.tk/otherpage2.html
Hello, this file doesn't matter. At all. It is a dummy.
I'm trying to replace a part of a page with external content on the fly.
Here is the source.html:
<!DOCTYPE html>
<html>
<head>
<%= foobar %>
</head>
<body>
This is body
</body>
</html>
And a replacement string inject.js:
var REGEXP = /^\'$/i; var foo = 1;
A ruby code that outputs a file by combining both.
pageContent = File.read('./source.html')
jsContent = File.read('./inject.js');
output = pageContent.gsub("<%= foobar %>", jsContent)
File.open('./dest.html', "w+") do |f|
f.write(output)
end
However, I get the messed up dest.html which is happening because of \' in inject.js.
<!DOCTYPE html>
<html>
<head>
var REGEXP = /^
</head>
<body>
This is body
</body>
</html>$/i; var foo = 1;
</head>
<body>
This is body
</body>
</html>
How do I get rid of this issue?
Try using gsub block form:
output = pageContent.gsub("<%= foobar %>") { jsContent }
This one can help you in this case.
Could you please try %q{jsContent} something like that.
I am trying to set a template variable with a string containing an umlaut. The body content is converted correctly, but the variable looses its special characters.
If I have a simple template var-template.md
<!DOCTYPE html>
<html>
<head>
<title>$title$</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<div>
$for(navi)$<button name="navi" value="$navi$">$navi$</button>$endfor$
</div>
<div>
$body$
</div>
</html>
and try to convert with variables set:
echo Hällo Wörld | pandoc -w html --template=var-template -V navi="Übung 1" -V title="Laß das döch"
it outputs:
<!DOCTYPE html>
<html>
<head>
<title>La� das d�ch</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<div>
<button name="navi" value="�bung 1">�bung 1</button>
</div>
<div>
<p>Hällo Wörld</p>
</div>
</html>
So the body text is converted correctly, but the variables are not.
Is this a bug or a mistake?
I am on ubuntu 12.04 with pandoc 1.9.1 ... do I have to manually update from the repository-version to a newer on?
I just tried this with the latest pandoc release, and it worked fine, so you may just need to upgrade. You should also make sure that your locale is UTF-8.