Skip to content

Commit

Permalink
Merge pull request #215 from orangekiwi-io/pdf_composer_integration
Browse files Browse the repository at this point in the history
Enhancement: PDF Composer migration from printpdf thanks to @orangekiwi-io
  • Loading branch information
sebastienrousseau authored Apr 15, 2024
2 parents 4480ae5 + 3bbd7da commit 2d06091
Show file tree
Hide file tree
Showing 17 changed files with 1,239 additions and 502 deletions.
1,402 changes: 1,181 additions & 221 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ name = "ssg"
readme = "README.md"
repository = "https://github.com/sebastienrousseau/shokunin"
rust-version = "1.71.1"
version = "0.0.27"
version = "0.0.28"
include = [
"/CONTRIBUTING.md",
"/LICENSE-APACHE",
Expand Down Expand Up @@ -51,9 +51,9 @@ comrak = "0.22.0"
dtt = "0.0.5"
env_logger = "0.11.3"
lazy_static = "1.4.0"
log = {version="0.4.21", features = ["std"] }
log = { version = "0.4.21", features = ["std"] }
minify-html = "0.15.0"
printpdf = "0.7.0"
pdf_composer = "0.2.61"
pulldown-cmark = "0.10.2"
quick-xml = "0.31.0"
regex = "1.10.4"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ following to your `Cargo.toml` file:

```toml
[dependencies]
shokunin = "0.0.27"
shokunin = "0.0.28"
```

Add the following to your `main.rs` file:
Expand Down
2 changes: 1 addition & 1 deletion content/404.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ viewport: "width=device-width, initial-scale=1, shrink-to-fit=no"
atom_link: "https://kaishi.one/404/rss.xml"
category: "Technology"
docs: "https://validator.w3.org/feed/docs/rss2.html"
generator: "Shokunin SSG (version 0.0.27)"
generator: "Shokunin SSG (version 0.0.28)"
item_description: "The page may have been removed or renamed. Please visit our homepage for more information."
item_guid: "https://kaishi.one/404/rss.xml"
item_link: "https://kaishi.one/404/rss.xml"
Expand Down
2 changes: 1 addition & 1 deletion content/contact.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ news_title: "Contact Us" ## The title of the page. (max 64 characters)
atom_link: https://kaishi.one/contact/rss.xml
category: "Technology"
docs: https://validator.w3.org/feed/docs/rss2.html
generator: "Shokunin SSG (version 0.0.27)"
generator: "Shokunin SSG (version 0.0.28)"
item_description: RSS feed for the contact page of the Kaishi website.
item_guid: https://kaishi.one/contact/rss.xml
item_link: https://kaishi.one/contact/rss.xml
Expand Down
2 changes: 1 addition & 1 deletion content/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ news_title: "Features" ## The title of the page. (max 64 characters)
atom_link: https://kaishi.one/rss.xml
category: "Technology"
docs: https://validator.w3.org/feed/docs/rss2.html
generator: "Shokunin SSG (version 0.0.27)"
generator: "Shokunin SSG (version 0.0.28)"
item_description: RSS feed for the site
item_guid: https://kaishi.one/rss.xml
item_link: https://kaishi.one/rss.xml
Expand Down
2 changes: 1 addition & 1 deletion content/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ news_title: "Kaishi, a Shokunin Static Site Generator Starter Template" ## The t
atom_link: https://kaishi.one/rss.xml
category: "Technology"
docs: https://validator.w3.org/feed/docs/rss2.html
generator: "Shokunin SSG (version 0.0.27)"
generator: "Shokunin SSG (version 0.0.28)"
item_description: RSS feed for the site
item_guid: https://kaishi.one/rss.xml
item_link: https://kaishi.one/rss.xml
Expand Down
2 changes: 1 addition & 1 deletion content/offline.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ news_title: "Offline" ## The title of the page. (max 64 characters)
atom_link: https://kaishi.one/rss.xml
category: "Technology"
docs: https://validator.w3.org/feed/docs/rss2.html
generator: "Shokunin SSG (version 0.0.27)"
generator: "Shokunin SSG (version 0.0.28)"
item_description: RSS feed for the site
item_guid: https://kaishi.one/rss.xml
item_link: https://kaishi.one/rss.xml
Expand Down
2 changes: 1 addition & 1 deletion content/post.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ news_title: "Posts" ## The title of the page. (max 64 characters)
atom_link: https://kaishi.one/rss.xml
category: "Technology"
docs: https://validator.w3.org/feed/docs/rss2.html
generator: "Shokunin SSG (version 0.0.27)"
generator: "Shokunin SSG (version 0.0.28)"
item_description: RSS feed for the site
item_guid: https://kaishi.one/rss.xml
item_link: https://kaishi.one/rss.xml
Expand Down
2 changes: 1 addition & 1 deletion content/privacy.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ news_title: "Privacy" ## The title of the page. (max 64 characters)
atom_link: https://kaishi.one/rss.xml
category: "Technology"
docs: https://validator.w3.org/feed/docs/rss2.html
generator: "Shokunin SSG (version 0.0.27)"
generator: "Shokunin SSG (version 0.0.28)"
item_description: RSS feed for the site
item_guid: https://kaishi.one/rss.xml
item_link: https://kaishi.one/rss.xml
Expand Down
2 changes: 1 addition & 1 deletion content/tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ news_title: "Tags" ## The title of the page. (max 64 characters)
atom_link: https://kaishi.one/rss.xml
category: "Technology"
docs: https://validator.w3.org/feed/docs/rss2.html
generator: "Shokunin SSG (version 0.0.27)"
generator: "Shokunin SSG (version 0.0.28)"
item_description: RSS feed for the site
item_guid: https://kaishi.one/rss.xml
item_link: https://kaishi.one/rss.xml
Expand Down
2 changes: 1 addition & 1 deletion content/terms.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ news_title: "Terms" ## The title of the page. (max 64 characters)
atom_link: https://kaishi.one/rss.xml
category: "Technology"
docs: https://validator.w3.org/feed/docs/rss2.html
generator: "Shokunin SSG (version 0.0.27)"
generator: "Shokunin SSG (version 0.0.28)"
item_description: RSS feed for the site
item_guid: https://kaishi.one/rss.xml
item_link: https://kaishi.one/rss.xml
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn build() -> Result<ArgMatches, Error> {
.author("Sebastien Rousseau")
.about("")
.bin_name("ssg")
.version("0.0.27")
.version("0.0.28")
.arg(
Arg::new("new")
.help("Create a new project.")
Expand Down
128 changes: 42 additions & 86 deletions src/compiler/service.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
// Copyright © 2024 Shokunin Static Site Generator. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

use pdf_composer::{
FontsStandard, PDFComposer, PDFDocInfoEntry, PDFVersion,
PaperOrientation, PaperSize,
};
use rlg::log_level::LogLevel::ERROR;

use crate::modules::pdf::generate_pdf;
// use crate::modules::pdf::generate_pdf;
use crate::{
macro_cleanup_directories, macro_create_directories,
macro_log_info, macro_metadata_option, macro_set_rss_data_fields,
models::data::{FileData, PageData, RssData},
metadata::service::extract_and_prepare_metadata,
models::data::{FileData, PageData, RssData},
modules::{
cname::create_cname_data,
html::generate_html,
Expand All @@ -18,8 +22,8 @@ use crate::{

navigation::NavigationGenerator,
news_sitemap::create_news_site_map_data,
pdf::PdfGenerationParams,
plaintext::generate_plain_text,
// pdf::PdfGenerationParams,
// plaintext::generate_plain_text,
rss::generate_rss,
sitemap::create_site_map_data,
tags::*,
Expand All @@ -31,7 +35,12 @@ use crate::{
write::write_files_to_build_directory,
},
};
use std::{collections::HashMap, error::Error, fs, path::Path};
use std::{
collections::HashMap,
error::Error,
fs,
path::{Path, PathBuf},
};

/// Compiles files in a source directory, generates HTML pages from them, and
/// writes the resulting pages to an output directory. Also generates an index
Expand Down Expand Up @@ -97,94 +106,41 @@ pub fn compile(
String::from("Fallback HTML content")
});

// Generate PDF
let (
plain_text,
plain_title,
plain_description,
plain_author,
plain_creator,
plain_keywords,
) = match generate_plain_text(
&file.content,
&macro_metadata_option!(metadata, "title"),
&macro_metadata_option!(metadata, "description"),
&macro_metadata_option!(metadata, "author"),
&macro_metadata_option!(metadata, "generator"),
&keywords.join(", "),
) {
Ok((
plain_text,
plain_title,
plain_description,
plain_author,
plain_creator,
plain_keywords,
)) => (
plain_text,
plain_title,
plain_description,
plain_author,
plain_creator,
plain_keywords,
),
Err(err) => {
let description = format!(
"Error generating Plain Text: {:?}",
err
);
macro_log_info!(
&ERROR,
"compiler.rs - Line 107",
&description,
&LogFormat::CLF
);
// Provide fallback values
(
String::from("Fallback Plain Text content"),
String::new(),
String::new(),
String::new(),
String::new(),
String::new(),
)
}
};

// Determine the filename without the extension
let filename_without_extension = Path::new(&file.name)
.file_stem()
.and_then(|stem| stem.to_str())
.unwrap_or(&file.name);
let common_path = build_dir_path.to_str().unwrap();
let pdf_path = if filename_without_extension == "index" {
format!("{}/", common_path)
} else {
format!(
"{}/{}/",
common_path, filename_without_extension
)

let mut pdf_source_paths: Vec<PathBuf> = Vec::new();
let source_for_pdf =
Path::new(content_path).join(&file.name);
pdf_source_paths.push(source_for_pdf);
let mut pdf_instance = PDFComposer::new();
pdf_instance.set_pdf_version(PDFVersion::V1_7);
pdf_instance.set_paper_size(PaperSize::A5);
pdf_instance.set_orientation(PaperOrientation::Landscape);
pdf_instance.set_margins("20");
pdf_instance.set_font(FontsStandard::TimesRoman);
pdf_instance.add_source_files(pdf_source_paths);
let author_entry = PDFDocInfoEntry {
doc_info_entry: "Author",
yaml_entry: "author",
};
if let Err(err) = generate_pdf(PdfGenerationParams {
plain_title: &plain_title,
plain_description: &plain_description,
plain_text: &plain_text,
plain_author: &plain_author,
plain_creator: &plain_creator,
plain_keywords: &plain_keywords,
output_dir: &pdf_path,
filename: filename_without_extension,
}) {
let description =
format!("Error generating PDF: {:?}", err);
macro_log_info!(
&ERROR,
"compiler.rs - Line 81",
&description,
&LogFormat::CLF
);
// Handle the error here, for example, return early or log it
}
let generator_entry = PDFDocInfoEntry {
doc_info_entry: "generator",
yaml_entry: "generator",
};
pdf_instance.set_doc_info_entry(author_entry);
pdf_instance.set_doc_info_entry(generator_entry);
let pdf_destination =
Path::new(common_path).join(filename_without_extension);
pdf_instance.set_output_directory(
pdf_destination.to_str().unwrap(),
);

pdf_instance.generate_pdfs();

// Create page options
let mut page_options = PageOptions::new();
Expand Down
7 changes: 2 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
//!
//! ```toml
//! [dependencies]
//! shokunin = "0.0.27"
//! shokunin = "0.0.28"
//! ```
//!
//! And in your `main.rs`:
Expand Down Expand Up @@ -112,8 +112,7 @@
#![crate_type = "lib"]

use crate::{
compiler::service::compile,
languages::translate,
compiler::service::compile, languages::translate,
loggers::init_logger, server::serve::start,
utilities::uuid::generate_unique_string,
};
Expand All @@ -122,7 +121,6 @@ use dtt::DateTime;
use rlg::{log_format::LogFormat, log_level::LogLevel, macro_log};
use std::{error::Error, fs::File, io::Write, path::Path};


/// The `cmd` module contains functions for the command-line interface.
pub mod cmd;

Expand All @@ -132,7 +130,6 @@ pub mod compiler;
/// The `lang` module contains the language translation functions.
pub mod lang;


/// The `languages` module contains the language translation functions.
pub mod languages;

Expand Down
3 changes: 0 additions & 3 deletions src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ pub mod preprocessor;
/// the `postprocessor` module contains functions for postprocessing content.
pub mod postprocessor;

/// The `pdf` module contains functions for generating PDFs.
pub mod pdf;

/// The `rss` module contains functions for generating RSS feeds.
pub mod rss;

Expand Down
Loading

0 comments on commit 2d06091

Please sign in to comment.