---
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()
```

```{bash}
#| label: git check
#| 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
```

```{bash}
#| label: git > github
#| eval: false

git pull

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

```{r}
#| label: pdf to jpeg
#| 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.jpeg", .) 
  
  pdf4pub[x] %>% 
    image_read_pdf() %>% 
    # image_transparent("red") %>% 
    image_write(format = "jpeg", file.path(path, filename))
  
})
```

```{r}
#| label: Images to PDF
#| 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"))
```

```{r}
#| label: Images to grids
#| 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
         )
```

```{r}
#| label: zip file
#| eval: false

tozip <- "manuscript" 

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

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