Skip to content

Commit

Permalink
examples: Improve webpage generation in various ways, add thumbnails,…
Browse files Browse the repository at this point in the history
… etc.
  • Loading branch information
icculus committed Dec 6, 2024
1 parent e50dc72 commit 90efb63
Show file tree
Hide file tree
Showing 53 changed files with 118 additions and 38 deletions.
133 changes: 100 additions & 33 deletions build-scripts/build-web-examples.pl
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,41 @@ sub build_latest {
}
}

sub get_category_description {
my $category = shift;
my $retval = ucfirst($category);

if (open(my $fh, '<', "$examples_dir/$category/description.txt")) {
$retval = <$fh>;
chomp($retval);
close($fh);
}

return $retval;
}

sub get_categories {
my @categories = ();

opendir(my $dh, $examples_dir) or die("Couldn't opendir '$examples_dir': $!\n");
foreach my $dir (sort readdir $dh) {
next if ($dir eq '.') || ($dir eq '..'); # obviously skip current and parent entries.
next if not -d "$examples_dir/$dir"; # only care about subdirectories.

push @categories, $dir;
if (open(my $fh, '<', "$examples_dir/categories.txt")) {
while (<$fh>) {
chomp;
s/\A\s+//;
s/\s+\Z//;
next if $_ eq '';
next if /\A\#/;
push @categories, $_;
}
close($fh);
} else {
opendir(my $dh, $examples_dir) or die("Couldn't opendir '$examples_dir': $!\n");
foreach my $dir (sort readdir $dh) {
next if ($dir eq '.') || ($dir eq '..'); # obviously skip current and parent entries.
next if not -d "$examples_dir/$dir"; # only care about subdirectories.
push @categories, $dir;
}
closedir($dh);
}
closedir($dh);

return @categories;
}
Expand Down Expand Up @@ -131,10 +155,16 @@ sub handle_example_dir {
$basefname = "$category-$basefname";
my $jsfname = "$basefname.js";
my $wasmfname = "$basefname.wasm";
my $thumbnailfname = 'thumbnail.png';
my $onmouseoverfname = 'onmouseover.webp';
my $jssrc = "$compile_dir/examples/$jsfname";
my $wasmsrc = "$compile_dir/examples/$wasmfname";
my $thumbnailsrc = "$examples_dir/$category/$example/$thumbnailfname";
my $onmouseoversrc = "$examples_dir/$category/$example/$onmouseoverfname";
my $jsdst = "$dst/$jsfname";
my $wasmdst = "$dst/$wasmfname";
my $thumbnaildst = "$dst/$thumbnailfname";
my $onmouseoverdst = "$dst/$onmouseoverfname";

my $description = '';
if (open(my $readmetxth, '<', "$examples_dir/$category/$example/README.txt")) {
Expand All @@ -150,6 +180,8 @@ sub handle_example_dir {
do_mkdir($dst);
do_copy($jssrc, $jsdst);
do_copy($wasmsrc, $wasmdst);
do_copy($thumbnailsrc, $thumbnaildst) if ( -f $thumbnailsrc );
do_copy($onmouseoversrc, $onmouseoverdst) if ( -f $onmouseoversrc );

my $highlight_cmd = "highlight '--outdir=$dst' --style-outfile=highlight.css --fragment --enclose-pre --stdout --syntax=c '--plug-in=$examples_dir/highlight-plugin.lua'";
print("$highlight_cmd\n");
Expand Down Expand Up @@ -184,11 +216,14 @@ sub handle_example_dir {
}
$other_examples_html .= "</ul>";

my $category_description = get_category_description($category);

my $html = '';
open my $htmltemplate, '<', "$examples_dir/template.html" or die("Couldn't open '$examples_dir/template.html': $!\n");
while (<$htmltemplate>) {
s/\@project_name\@/$project/g;
s/\@category_name\@/$category/g;
s/\@category_description\@/$category_description/g;
s/\@example_name\@/$example/g;
s/\@javascript_file\@/$jsfname/g;
s/\@htmlified_source_code\@/$htmlified_source_code/g;
Expand All @@ -203,6 +238,51 @@ sub handle_example_dir {
close($htmloutput);
}

sub generate_example_thumbnail {
my $project = shift;
my $category = shift;
my $example = shift;

my $example_no_num = "$example";
$example_no_num =~ s/\A\d+\-//;

my $example_image_url;
if ( -f "$examples_dir/$category/$example/thumbnail.png" ) {
$example_image_url = "/$project/$category/$example/thumbnail.png";
} elsif ( -f "$examples_dir/$category/thumbnail.png" ) {
$example_image_url = "/$project/$category/thumbnail.png";
} else {
$example_image_url = "/$project/thumbnail.png";
}

my $example_mouseover_html = '';
if ( -f "$examples_dir/$category/$example/onmouseover.webp" ) {
$example_mouseover_html = "onmouseover=\"this.src='/$project/$category/$example/onmouseover.webp'\" onmouseout=\"this.src='$example_image_url'\";";
} elsif ( -f "$examples_dir/$category/onmouseover.webp" ) {
$example_mouseover_html = "onmouseover=\"this.src='/$project/$category/onmouseover.webp'\" onmouseout=\"this.src='$example_image_url'\";";
}

return "
<a href='/$project/$category/$example'>
<div>
<img src='$example_image_url' $example_mouseover_html />
<div>$example_no_num</div>
</div>
</a>"
;
}

sub generate_example_thumbnails_for_category {
my $project = shift;
my $category = shift;
my @examples = get_examples_for_category($category);
my $retval = '';
foreach my $example (@examples) {
$retval .= generate_example_thumbnail($project, $category, $example);
}
return $retval;
}

sub handle_category_dir {
my $category = shift;

Expand All @@ -220,26 +300,22 @@ sub handle_category_dir {

closedir($dh);

my $examples_list_html = "";
foreach my $example (get_examples_for_category($category)) {
# !!! FIXME: image
my $example_image_url = "/$project/placeholder.png";
$examples_list_html .= "
<a href='/$project/$category/$example'>
<div>
<img src='$example_image_url' />
<div>$category/$example</div>
</div>
</a>";
}
my $examples_list_html = generate_example_thumbnails_for_category($project, $category);

# write category page
my $dst = "$output_dir/$category";

do_copy("$examples_dir/$category/thumbnail.png", "$dst/thumbnail.png") if ( -f "$examples_dir/$category/thumbnail.png" );
do_copy("$examples_dir/$category/onmouseover.webp", "$dst/onmouseover.webp") if ( -f "$examples_dir/$category/onmouseover.webp" );

my $category_description = get_category_description($category);

# write category page
my $html = '';
open my $htmltemplate, '<', "$examples_dir/template-category.html" or die("Couldn't open '$examples_dir/template-category.html': $!\n");
while (<$htmltemplate>) {
s/\@project_name\@/$project/g;
s/\@category_name\@/$category/g;
s/\@category_description\@/$category_description/g;
s/\@examples_list_html\@/$examples_list_html/g;
$html .= $_;
}
Expand Down Expand Up @@ -276,7 +352,7 @@ sub handle_category_dir {
build_latest();

do_copy("$examples_dir/template.css", "$output_dir/examples.css");
do_copy("$examples_dir/template-placeholder.png", "$output_dir/placeholder.png");
do_copy("$examples_dir/template-placeholder.png", "$output_dir/thumbnail.png");

opendir(my $dh, $examples_dir) or die("Couldn't opendir '$examples_dir': $!\n");

Expand All @@ -292,19 +368,10 @@ sub handle_category_dir {
# write homepage
my $homepage_list_html = "";
foreach my $category (get_categories()) {
$homepage_list_html .= "<h2>$category</h2>";
my $category_description = get_category_description($category);
$homepage_list_html .= "<h2>$category_description</h2>";
$homepage_list_html .= "<div class='list'>";
foreach my $example (get_examples_for_category($category)) {
# !!! FIXME: image
my $example_image_url = "/$project/placeholder.png";
$homepage_list_html .= "
<a href='/$project/$category/$example'>
<div>
<img src='$example_image_url' />
<div>$category/$example</div>
</div>
</a>";
}
$homepage_list_html .= generate_example_thumbnails_for_category($project, $category);
$homepage_list_html .= "</div>";
}

Expand Down
Binary file added examples/asyncio/01-load-bitmaps/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/asyncio/description.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Asynchronous I/O
Binary file added examples/audio/onmouseover.webp
Binary file not shown.
Binary file added examples/audio/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file added examples/camera/01-read-and-draw/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions examples/categories.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Blank lines and lines that start with '#' in this file are ignored.

# Categories, by directory name, go in here, in the order they should be
# listed on the main page. If this file is missing, it'll assume any
# subdirectory is a category and sort them alphabetically.

renderer
input
audio
camera
asyncio
pen
demo
Binary file added examples/demo/01-snake/onmouseover.webp
Binary file not shown.
Binary file added examples/demo/01-snake/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/demo/02-woodeneye-008/onmouseover.webp
Binary file not shown.
Binary file added examples/demo/02-woodeneye-008/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file added examples/demo/03-infinite-monkeys/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/demo/04-bytepusher/onmouseover.webp
Binary file not shown.
Binary file added examples/demo/04-bytepusher/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/demo/description.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Full game and app demos
Binary file not shown.
Binary file added examples/input/01-joystick-polling/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file added examples/input/02-joystick-events/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/pen/01-drawing-lines/onmouseover.webp
Binary file not shown.
Binary file added examples/pen/01-drawing-lines/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/renderer/01-clear/onmouseover.webp
Binary file not shown.
Binary file added examples/renderer/01-clear/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/renderer/02-primitives/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/renderer/03-lines/onmouseover.webp
Binary file not shown.
Binary file added examples/renderer/03-lines/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/renderer/04-points/onmouseover.webp
Binary file not shown.
Binary file added examples/renderer/04-points/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file added examples/renderer/05-rectangles/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/renderer/06-textures/onmouseover.webp
Binary file not shown.
Binary file added examples/renderer/06-textures/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/renderer/10-geometry/onmouseover.webp
Binary file not shown.
Binary file added examples/renderer/10-geometry/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/renderer/11-color-mods/onmouseover.webp
Binary file not shown.
Binary file added examples/renderer/11-color-mods/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/renderer/14-viewport/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/renderer/15-cliprect/onmouseover.webp
Binary file not shown.
Binary file added examples/renderer/15-cliprect/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file added examples/renderer/17-read-pixels/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/renderer/18-debug-text/thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions examples/template-category.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>@project_name@ Examples: @category_name@</title>
<title>@project_name@ Examples: @category_description@</title>
<link
rel="stylesheet"
type="text/css"
Expand All @@ -18,7 +18,7 @@
</head>
<body>
<header>
<a href="/">SDL Examples</a>
<a href="/">@project_name@ Examples</a>
</header>
<main>
<nav class="breadcrumb">
Expand All @@ -27,7 +27,7 @@
<li><a href="/@project_name@/@category_name@">@category_name@</a></li>
</ul>
</nav>
<h1>@project_name@ examples: @category_name@</h1>
<h1>@project_name@ examples: @category_description@</h1>
<div class="list">@examples_list_html@</div>
</main>
</body>
Expand Down
2 changes: 0 additions & 2 deletions examples/template-homepage.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
</nav>
<h1>@project_name@ examples</h1>

<p>Check out the @project_name@ examples here!</p>

@homepage_list_html@
</main>
</body>
Expand Down
Binary file modified examples/template-placeholder.png

0 comments on commit 90efb63

Please sign in to comment.