---
format:
  html:
    css: "files/style.css"
    toc: true
    toc-location: left
    number-sections: true
    self-contained: true
  docx:
    toc: false
    toc-title: Indice
    section-numbers: true
    reference-doc: "files/style.docx"
editor_options: 
  chunk_output_type: console
execute:
  echo: false
  warning: false
crossref:
  fig-title: Figure
  fig-prefix: Figure
  tbl-title: Table
  tbl-prefix: Table
---

```{r}
#| label: setup

source("http://inkaverse.com/docs.r")
```

```{r}
#| results: 'asis'

"render2rticle.md" %>% 
  rticle(type = "asis") %>% # ["asis" or "list"]
  knitr::knit_child(quiet = TRUE) %>% 
  cat(sep = '\n')
```

```{r}
knitr::knit_exit()
```

# GitHub: check files

```{bash}
#| eval: false

SIZE_MB=100
EXCLUDE_DIRS=(".git" "xclude")

find . \
  \( -name "${EXCLUDE_DIRS[0]}" -o -name "${EXCLUDE_DIRS[1]}" \) \
  -prune -o \
  -type f -size +${SIZE_MB}M -exec ls -lh {} \; | sort -hr -k5
  
git reset --soft HEAD~0
git status
```

# GitHub: update repository

```{bash}
#| eval: false

git pull

git add .
git commit -m "update"
git push
```

# PDF to images (.jpg)

```{r}
#| eval: false

library(tidyverse)
library(magick)

pdf4pub <- list.files(".", full.names = T, recursive = T
                      , pattern = "img.+.pdf") 

path <- dirname(pdf4pub)

imgs <- 1:length(pdf4pub) %>% map(\(x) {
  
  filename <- pdf4pub[x] %>% 
    basename() %>% 
    gsub(".pdf", "\\1.jpg", .) 
  
  pdf4pub[x] %>% 
    image_read_pdf() %>% 
    # image_transparent("red") %>% 
    image_write(format = "jpg", file.path(path, filename))
  
})
```

# Images to PDF

```{r}
#| eval: false

library(tidyverse)
library(magick)
library(qpdf)

img2pdf <- list.files(".", full.names = T
                      , recursive = T
                      , pattern = "images") %>% 
  image_read() %>% 
  image_write(format = "pdf"
              , file.path(getwd(), "figures.pdf"))
```

# Images to grid

```{r}
#| eval: false

library(tidyverse)
library(magick)
library(cowplot)

pdf4pub <- list.files(".", full.names = T, recursive = T
                      , pattern = "FigureX") %>% 
  image_read(., density = 600)
  
grid <- list(
  pdf4pub[1] %>% image_crop(image = ., "0x0+0+1500")
  , pdf4pub[2]
  ) %>%  
  map( ~ image_ggplot(.)) %>% 
  plot_grid(plotlist = ., labels = "auto", ncol = 2)

plot <- grid %>% 
  ggsave(plot = ., "manuscript/Figure-X.jpg"
         , units = "cm"
         , width = 20
         , height = 10
         )
```

# Zip files

```{r}
#| eval: false

tozip <- "manuscript" 

# unlink(paste0(tozip, ".zip"), recursive = T)

utils::zip(zipfile = paste0(tozip, ".zip")
                             , files = tozip
                             , flags = "-r9Xj"
                             )
```

