-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8db6ecc
commit c8be412
Showing
123 changed files
with
7,043 additions
and
4,468 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-7.2 KB
(96%)
Ciencia-com-R_files/figure-html/teorema-central-limite-moeda-plot-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
ParseRmdContent <- function(rmd_files, bib, k) { | ||
# parse Rmd files to get questions, answers, section and chapter | ||
rmd_rawdata <- parsermd::parse_rmd(rmd_files[k], allow_incomplete = FALSE, parse_yaml = FALSE) |> | ||
tibble::as_tibble() | ||
|
||
# clean R chunks and headings | ||
rmd_data <- rmd_rawdata[rmd_rawdata$type != "rmd_chunk", ] | ||
rmd_data <- rmd_data[rmd_data$type != "rmd_heading", ] | ||
|
||
# keep rows with complete headers 1 to 3 | ||
rmd_data <- rmd_data[complete.cases(dplyr::select(rmd_data, c("sec_h1", "sec_h2", | ||
"sec_h3"))), ] | ||
|
||
# loop over all answers | ||
for (i in 1:nrow(rmd_data)) { | ||
chapter <- rmd_data[i, ]$sec_h1 | ||
# remove ** from chapter | ||
chapter <- gsub("\\*\\*", "", chapter) | ||
# remove {-} from chapter | ||
chapter <- gsub("\\{-\\}", "", chapter) | ||
|
||
section <- rmd_data[i, ]$sec_h2 | ||
# remove {-} from section | ||
section <- gsub("\\{-\\}", "", section) | ||
|
||
question <- rmd_data[i, ]$sec_h3 | ||
|
||
answers <- rmd_data[i, ] |> | ||
parsermd::as_document() | ||
# keep only rows starting with '- ' | ||
answers <- answers[grep("^- ", answers)] | ||
# remove - from answers | ||
answers <- gsub("^- ", "", answers) | ||
# replace $$ by $ from LaTeX equations | ||
answers <- gsub("\\$\\$", "$", answers) | ||
# replacE $ in begining of any word string by $\\ | ||
answers <- gsub("\\$([a-zA-Z])", "$\\\\\\1", answers) | ||
# replace $\\ by $ from answers | ||
answers <- gsub("\\$\\\\", "$", answers) | ||
|
||
# get content within brackets starting with @ [@] | ||
key <- gsub(".*\\[@(.*?)\\].*", "\\1", answers) | ||
|
||
# get source from answers [@key] | ||
answers <- trimws(gsub("\\[@(.*?)\\]", "", answers), which = "left") | ||
|
||
entry <- data.frame(chapter = rep(chapter, length(answers)), section = rep(section, | ||
length(answers)), question = rep(question, length(answers)), answer = answers, | ||
key = key) | ||
|
||
if (nrow(entry) != 0) { | ||
for (j in 1:nrow(entry)) { | ||
# save post to PNG | ||
SavePost(entry[j, ], bib) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
SavePost <- function(entry, bib) { | ||
chapter <- entry$chapter | ||
section <- entry$section | ||
question <- entry$question | ||
answer <- entry$answer | ||
key <- entry$key | ||
|
||
# detect if there is a r`` code block | ||
if (grepl("`r", answer)) { | ||
# extract code block | ||
code <- gsub(".*`r", "", answer) | ||
code <- gsub("`.*", "", code) | ||
# evaluate code | ||
code.eval <- eval(parse(text = code)) | ||
# replace code block with result | ||
answer <- gsub("`r.*`", code.eval, answer) | ||
} | ||
|
||
# double $ to do not convert to math | ||
answer <- gsub("\\$", "$$", answer) | ||
# convert markdown to HTML text | ||
answer <- markdown::markdownToHTML(text = answer, fragment.only = TRUE) | ||
# convert back $$ to $ | ||
answer <- gsub("\\$\\$", "$", answer) | ||
# remove HTML <p> tags | ||
answer <- gsub("<p>", "", answer) | ||
answer <- gsub("</p>", "", answer) | ||
# remove \n t the end | ||
answer <- gsub("\n", "", answer) | ||
|
||
# function to convert italic and bold to tex | ||
emphasis <- function(text) { | ||
text <- gsub("<em>", "\\\textit{", text) | ||
text <- gsub("</em>", "}", text) | ||
text <- gsub("<strong>", "\\\textbf{", text) | ||
text <- gsub("</strong>", "}", text) | ||
return(text) | ||
} | ||
|
||
question <- emphasis(question) | ||
answer <- emphasis(answer) | ||
|
||
source <- bib[key] | ||
citation <- format(source, style = "text") | ||
citation <- gsub("\\[1\\]", "", citation) | ||
# remove underline character | ||
citation <- gsub("_", "", citation) | ||
# remove content within < >. | ||
citation <- gsub("<.*?>.", "", citation) | ||
# merge citations | ||
citation <- paste0(citation, collapse = "\\") | ||
|
||
# Ensure chapter folder exists | ||
chapter_folder <- file.path(getwd(), "posts", chapter) | ||
dir.create(chapter_folder, showWarnings = FALSE, recursive = TRUE) | ||
|
||
# Ensure section folder exists | ||
section_folder <- file.path(chapter_folder, section) | ||
dir.create(section_folder, showWarnings = FALSE, recursive = TRUE) | ||
|
||
# File name based on question | ||
file_name <- paste0(gsub(" ", "_", question), ".png") | ||
# add a number to file name | ||
file_name <- paste0(as.character(length(list.files(section_folder, pattern = ".png"))), | ||
"_", file_name) | ||
file_path <- file.path(section_folder, file_name) | ||
|
||
# read tex file | ||
tex <- readLines(file.path("tex", "POST.tex")) | ||
|
||
# replace CAPITULO with chapter | ||
tex <- gsub("CAPITULO", chapter, tex) | ||
# replace SECAO with section | ||
tex <- gsub("SECAO", section, tex) | ||
# replace QUESTAO with question | ||
tex <- gsub("QUESTAO", question, tex) | ||
# replace RESPOSTA with answer | ||
tex <- gsub("RESPOSTA", answer, tex) | ||
# replace CITACAO with citation | ||
tex <- gsub("CITACAO", citation, tex) | ||
# escape & character | ||
tex <- gsub("\\&", "\\\\&", tex) | ||
# escape % character | ||
tex <- gsub("\\%", "\\\\%", tex) | ||
|
||
# compile tex | ||
writeLines(tex, file.path("posts", "POST.tex")) | ||
|
||
# render text file to pdf | ||
tools::texi2pdf(file.path("posts", "POST.tex"), clean = TRUE) | ||
|
||
# move pdf to posts | ||
file.rename(from = file.path("POST.pdf"), to = file.path("posts", "POST.pdf")) | ||
|
||
# render PDF to PNG file | ||
bitmap <- pdftools::pdf_render_page(file.path("posts", "POST.pdf"), dpi = 300) | ||
png::writePNG(bitmap, file_path) | ||
|
||
img <- magick::image_read(file_path) | ||
cover <- magick::image_read(file.path(getwd(), "images", "Cover_1.png")) | ||
cover <- magick::image_scale(cover, "400x400") # Ajuste o tamanho conforme necessário | ||
img <- magick::image_composite(img, cover, offset = "+1200+0") # Ajuste a posição conforme necessário | ||
magick::image_write(img, path = file_path, format = "png", quality = 100, density = 300) | ||
|
||
# delete pdf and tex files | ||
file.remove(file.path("posts", "POST.pdf")) | ||
file.remove(file.path("posts", "POST.tex")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.