Link the CSS file to the HTML one in CGI programm - html

So, I recently started to code a web interface with CGI, after finding how to print hello world on my Apache web page with HTML, I wanted to add some CSS to it but apparently it doesn't work.
I'm on Linux, and I am working with Apache2.
So there is my source file hello.c (which is in /usr/lib/cgi-bin):
#include <stdio.h>
int main()
{
printf(
"Context-type:text/html\n\n"
"<html>\n"
"<head>\n"
"<title> Hi world </title>\n"
"<link type=\"text/css\" rel=\"stylesheet\" href=\"style/style.css\">\n"
"</head>\n"
"<body>\n"
"<h2>Hello world !</h2>\n"
"</body>\n"
"</html>\n"
);
return 0;
}
and there is my CSS file (which is in /usr/lib/cgi-bin/style/):
body {
background-color:rgb(255, 0, 170);
}
h2 {
font-family: Times;
font-size: 38pt;
}
I already tried to put my CSS file in my home directory but it doesn't work too.

If you check your network panel of your browser's debugger, you're probably encountering a 500 status, as the server refuses to serve /cgi-bin/style/style.css.
Generally, style sheets do not belong in the CGI directory. You should move them to somewhere in your DocumentRoot path.
Assuming your configuration is something like:
DocumentRoot "/var/www"
ScriptAlias /cgi-bin/ "/usr/lib/cgi-bin/"
Set your directory structure to:
/usr/lib/
└── cgi-bin
└── hello.cgi
/var/www
└── style
└── style.css
Then change the relative path
"<link type=\"text/css\" rel=\"stylesheet\" href=\"style/style.css\">\n"
to an absolute path (note the / prefix):
"<link type=\"text/css\" rel=\"stylesheet\" href=\"/style/style.css\">\n"

Related

Referencing variables in an SCSS root file from a module file

I'm very new to SCSS so I'm still learning the basics.
I'm simply wondering if you can access variables and mixins in a parent file from a child module. It's kind of hard to put into words so I'll give an example below..
File: app.scss
#use base;
$main_color: red;
File: _base.scss
#use app;
body {
color: app.main_color;
}
The idea is that you could have a root file with a bunch of base styles that the imported modules could reference... is this a thing? If so how do you do it?
You need to change _base.scss in this way:
#import "app";
body {
color: $main_color;
}

Jekyll can't process scss

I am trying to follow the step by step tutorial here
Here is my structure :
_config.yml _data/ _includes/ _layouts/ _sass/ 404.html about.md assets/ Gemfile Gemfile.lock index.html
and _sass :
ls _sass/
main.scss
and assets/css :
ls assets/css/
styles.scss
and when I jekyll serve I get :
Conversion error: Jekyll::Converters::Scss encountered an error while converting 'assets/css/styles.scss':
File to import not found or unreadable: main. on line 1
So somehow jekyll is not able to find the main.scss file but I don't understand why since apparently everything is set up as it should be. Any idea ?
Also here is my style.scss:
---
---
#import "main";
and my main.scss :
.current {
color: green;
}
If I change styles.scss to :
---
---
.current {
color: green;
}
Then it works since it does not try to import main.scss. I tried with/without _config.yml for specifying the sass directory. This does not change anything.

How to remove a file if it is not used by another file

I have to clean the directory and its subdirectories by removing all unused files. (A file is considered unused if it is not linked to in any
of the HTML files or if it is not specified explicitly that this file is in use). A file can be linked in an HTML file by either href or img src.
For example, I have an I.html,1.html,2.html and 1 folder. In I.html file, an href uses 1.html and 1 directory, but 2.html is not used by any other files. So, how can I remove the unused 2.html file?
use strict;
use warnings;
my($path,$regexExpression) = #ARGV;
my $fileNames = "data.txt";
my #abc= ();
if(not defined $path){
die "File directory not given, please try again \n"
}
print "added file ";
if (not defined $regexExpression) {
$regexExpression="*";
print "--Taking default Regular Expression. \n"
}
if (defined $regexExpression) {
print "The regular Expression : $regexExpression \n";
my $directorypathx= `pwd`;
my ($listofFileNames) = findFilesinDir($path);
my ($listofLinks) = readallHrefInaFile();
my ($listofImage) = readImageFile();
print $listofLinks;
}
sub findFilesinDir{
print "inside subroutines ", $path,"\n";
my($pathName) = #_;
my $fileNames =`find '$pathName' -name '$regexExpression' | sort -h -r > $fileNames ` ;
if (-l $fileNames){
return $fileNames;
}
}
sub readallHrefInaFile{
my $getAllLinks = ` grep -Eo "<a .*href=.*>" $path*.html | uniq ` ;
push (#abc,$getAllLinks);
}
sub readImageFile{
print "image files \n";
my $getAllImage = ` grep -Eo "<img .*src=.*>" $path*.html | uniq `;
push (#abc,$getAllImage);
}
print #abc;
I.html
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>Index</h1>
1
<h1>Downloads</h1>
Compressed craters
<hr>
</body>
</html>
1.html
<html>
<head>
<title>1</title>
</head>
<body>
<h1>1</h1>
<img src="images/1-1.gif" />
<img src="images/1-2.gif" />
<hr>
</body>
</html>
The overall approach you show is reasonable, but there is a lot to say about the code itself. The place to do that would be code review and I encourage you to submit your code there as well.
One overall comment I'd make is that there is no reason to reach so often for external tools; your program uses external grep and find and sort and pwd. We can practically always do the whole job with an abundance of tools that Perl provides.
Here is a simple example for what you need, where most of work is done using modules.
The list of files to search for in our HTML is assembled using File::Find::Rule, recursively under $dir. Another option is the core File::Find module.
Even as HTML parsing appears simple in this case, it is much better to use a module for that as well, instead of a regex. The HTML::TreeBuilder is a bit of a standard for what you need here. That module itself uses others, the workhorse being HTML::Element
The following program works with one HTML file ($source_file), for which we need to find files under a given directory ($dir) which are not used in either an href attribute or a src attribute in img tag. These files need be deleted (that line is commented out).
use warnings;
use strict;
use feature 'say';
use File::Find::Rule;
use HTML::TreeBuilder;
my ($dir, $source_file) = #ARGV;
die "Usage: $0 dir-name file-name\n" if not $dir or not $source_file;
my #files = File::Find::Rule->file->in($dir);
#say for #files;
foreach my $file (#files) {
next if $file eq $source_file; # not the file itself!
say "Processing $file...";
my $tree = HTML::TreeBuilder->new_from_file($source_file);
my $esc_file = quotemeta $file;
my #in_href = $tree->look_down( 'href', qr/$esc_file/ );
my #in_img_src = $tree->look_down( _tag => 'img', 'src', qr/$esc_file/ );
if (#in_href == 0 and #in_img_src == 0) {
say "\tthis file is not used in 'href' or 'img-src' in $source_file";
# To delete it uncomment the next line -- after all is fully tested
#unlink $file or warn "Can't unlink $file: $!";
}
}
The statement that actually removes files, using unlink, is of course commented out. Enable that only once you have thoroughly checked the final version of the script, and have made backups.
Notes
Refine what files you are looking for by adding "rules" with File::Find::Rule
I use quotemeta on filenames, which escapes all special characters in them; otherwise something may sneak in that would throw off the regex used by look_down
The code above simply parses twice through each file, assembling the lists of elements found for href attribute and then for src attribute (in img tag). This can be done in one pass, by using sub { } specification for criteria in look_down
The script must be invoked with the directory name and the main HTML file name. Please change that for proper command line parsing, and more sophisticated use, with Getopt::Long
A whole lot more can be fine tuned here, both with searching for files and in parsing HTML; there is a lot of information in modules' documentation, and yet more in many posts around this site.
The code is tested for simple cases; please adjust to your realistic needs.
Here is a full example of usage.
I place this script (script.pl) in a directory with a file I.html and a directory www.
The I.html file:
<!DOCTYPE html>
<html> <head> <title>Test handling of unused files</title> </head>
<body>
Used file from www
<img src="www/images/used.jpg" alt="no_image_really">
</body>
</html>
The directory www has files used.html and another.html, and a subdirectory images with files used.jpg and another.jpg in it, so altogether we have
.
├── script.pl
├── I.html
└── www
├── used.html
├── another.html
└── images
├── used.jpg
└── another.jpg
There is no need for any content in any of files in www for this test. This is only a minimal setup; I've added more files and directories, and tags to I.html, to test.
Then I run script.pl www I.html and get the expected output.

Nginx: Replacing response text, with text from another text/JSON file

In the following HTML file served via NGINX:
<html>
<body>Today is {{day}}</body>
</html>
I have the following JSON file:
{
"year": "2015",
"month": "July",
"day": "Thursday"
}
I would like the output to be modified by NGINX to the following:
<html>
<body>Today is Thursday</body>
</html>
I have looked into searching the response text via regex and replacing it, using the module: NGINX using: http://nginx.org/en/docs/http/ngx_http_sub_module.html
The question is, how do I read the JSON file located on the same server as NGINX, and map the replacements?
P.s.: If needed, I can make amends to using plain text file for the mappings, instead of JSON file.
As a reference for other searchers, you can use the SSI module of NGINX for this.
You would need the variables in your NGINX config:
location = /today.html {
ssi on;
set $year '2015';
set $month 'July';
set $day 'Thursday';
root /var/www/default;
}
And in the template file (/var/www/default/today.html):
<html>
<body>Today is <!--# echo var="day" --></body>
</html>
I can make amends to using plain text file for the mappings, instead of JSON file
I don't now if you can do this via JSON, but it should be possible with plain text files. You can include the plain text file in the template (docs):
<html>
<body>
<!--# include file="day.txt" -->
</body>
</html>
Where day.txt should contain
Thursday
You can also include a script that return the current day, so you don't need to update day.txt.

Textbrowser displaying an image showing "QFSEngine::open: No file specified " warning

I´m using a QTextBrowser to display an external html document (and its image resources) which is placed in the same directory as the application.The relative paths don't work at all and If I type in the absolute file path of the image, it is displayed fine but a warning appears before the html document is loaded. The warning says "QFSEngine::open: No file specified" . i'm loading my html file using this snippet :
QFile file(QApplication::applicationDirPath().append("/test.html"));
if(!file.open(QIODevice::ReadWrite|QIODevice::Text))
return;
QTextStream in(&file);
ui->textBrowser->setHtml(in.readAll());
file.close();
and this is my html file:
<!doctype html>
<html>
<img src="test.png">
<p>paragraph which contains some text</p>
</html>
Am i going wrong in reading the html or is there anything i have to include in my cpp file?
You need to call QTextBrowser::setSearchPaths with full path to your .html.