Skip to content

Commit

Permalink
Cambios y mejoras desde apuntes de D3D
Browse files Browse the repository at this point in the history
Ni la generación de tareas es correcta ni el docstats
  • Loading branch information
jesustorresdev committed Feb 19, 2021
1 parent 21498ae commit 7cc8c75
Show file tree
Hide file tree
Showing 20 changed files with 500 additions and 173 deletions.
50 changes: 26 additions & 24 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
name: Generar, testear y publicar
name: Publicar

on:
push:
branches-ignore: [master]
pull_request:
branches-ignore: [master]
workflow_dispatch:
branches-ignore: [master]
push:
tags:
- '*'

jobs:
build-and-publish:
Expand All @@ -18,35 +16,39 @@ jobs:
with:
persist-credentials: false

- name: Extract el nombre de la rama
uses: nelonoel/branch-name@v1
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true

- name: Configurar Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.7

- name: Instalar dependencias
run: bundle install
run: |
sudo apt-get update -y -qq
sudo apt-get install pkg-config libxml2-dev libxslt-dev default-jre graphviz bison flex libffi-dev libgdk-pixbuf2.0-dev libcairo2-dev libpango1.0-dev fonts-lyx cmake
bundle config build.nokogiri --use-system-libraries
bundle install
- name: Generar la documentación
run: bundle exec rake build

- name: Testear
run: bundle exec rake tests

- name: Publicar
if: |
github.event != 'pull_request' || (
github.event.pull_request.types == 'closed' &&
github.event.pull_request.merged_by != ''
)
run: bundle exec rake tests:all

- name: Generar la documentación
run: bundle exec rake build:html build:pdf

- name: Publicar web
uses: JamesIves/[email protected]
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BRANCH: gh-pages
FOLDER: output/html
TARGET_FOLDER: ${{ env.BRANCH_NAME }}
TARGET_FOLDER: ${{ github.ref }}
CLEAN: true

- name: Publicar como 'release'
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: output/pdf/{*/*.pdf,*.zip}
tag: ${{ github.ref }}
file_glob: true
overwrite: true
prerelease: true
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea/
*.iml
.vscode
output/
.vscode/
*~
output/
stem-*
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ source "https://rubygems.org"

gem "rake"
gem "asciidoctor"
gem "asciidoctor-diagram"
gem "asciidoctor-mathematical"
gem "asciidoctor-pdf"
gem "asciidoctor-epub3", "~> 1.5.0.alpha.18"
gem "rouge"
Expand Down
15 changes: 15 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ GEM
public_suffix (>= 2.0.2, < 5.0)
afm (0.2.2)
asciidoctor (2.0.12)
asciidoctor-diagram (2.0.5)
asciidoctor (>= 1.5.7, < 3.x)
asciidoctor-epub3 (1.5.0.alpha.19)
asciidoctor (>= 1.5.6, < 3.0.0)
gepub (~> 1.0.0)
mime-types (~> 3.0)
asciidoctor-mathematical (0.3.5)
asciidoctor (~> 2.0)
asciimath (~> 2.0)
mathematical (~> 1.6.0)
asciidoctor-pdf (1.5.3)
asciidoctor (>= 1.5.3, < 3.0.0)
concurrent-ruby (~> 1.1.0)
Expand All @@ -22,6 +28,7 @@ GEM
thread_safe (~> 0.3.0)
treetop (~> 1.6.0)
ttfunk (~> 1.5.0, >= 1.5.1)
asciimath (2.0.2)
concurrent-ruby (1.1.7)
css_parser (1.7.1)
addressable
Expand All @@ -40,6 +47,10 @@ GEM
rainbow (~> 3.0)
typhoeus (~> 1.3)
yell (~> 2.0)
i18n (1.8.7)
concurrent-ruby (~> 1.0)
mathematical (1.6.14)
ruby-enum (~> 0.4)
mercenary (0.4.0)
mime-types (3.3.1)
mime-types-data (~> 3.2015)
Expand Down Expand Up @@ -75,6 +86,8 @@ GEM
rainbow (3.0.0)
rake (13.0.3)
rouge (3.26.0)
ruby-enum (0.8.0)
i18n
ruby-rc4 (0.1.5)
rubyzip (2.3.0)
safe_yaml (1.0.5)
Expand All @@ -91,7 +104,9 @@ PLATFORMS

DEPENDENCIES
asciidoctor
asciidoctor-diagram
asciidoctor-epub3 (~> 1.5.0.alpha.18)
asciidoctor-mathematical
asciidoctor-pdf
html-proofer
rake
Expand Down
148 changes: 6 additions & 142 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,143 +1,7 @@
require 'json'
require "nokogiri"
Rake.add_rakelib 'lib/tasks'

READING_SPEED_IN_WORDS_PER_MINUTE = 200

PROJECT_DIR = Rake.application.original_dir

CONTENT_DIR = File.join(PROJECT_DIR, "content")
CONFIG_DIR = File.join(PROJECT_DIR, "config")
OUTPUT_DIR = File.join(PROJECT_DIR, "output")

DOCUMENT_MAIN_FILE = File.join(CONTENT_DIR, "main.adoc")
DOCSTATS_FILE = File.join(CONTENT_DIR, "docstats.adoc")

INCLUDED_FILES = FileList[File.join(CONTENT_DIR, "**/*.adoc"),
File.join(CONFIG_DIR, "**/*.adoc"),
File.join(CONFIG_DIR, "**/*.yml")
].exclude(DOCUMENT_MAIN_FILE)

MEDIA_FILES = FileList[File.join(CONTENT_DIR, "**/media/**", "*.jpg"),
File.join(CONTENT_DIR, "**/media/**", "*.png"),
File.join(CONTENT_DIR, "**/media/**", "*.svg")]

OUTPUT_HTML_FILE = File.join(OUTPUT_DIR, "html", "index.html")
OUTPUT_HTML_DIR = File.dirname(OUTPUT_HTML_FILE)

OUTPUT_PDF_FILE = File.join(OUTPUT_DIR, "pdf", "sistemas-operativos.pdf")
OUTPUT_PDF_DIR = File.dirname(OUTPUT_PDF_FILE)

OUTPUT_EPUB_FILE = File.join(OUTPUT_DIR, "epub", "sistemas-operativos.epub")
OUTPUT_EPUB_DIR = File.dirname(OUTPUT_EPUB_FILE)

task :default => :build
task :build => [:docstats, :html]

desc 'Generar la versión en HTML de la documentación'
task :html => [OUTPUT_HTML_FILE, :html_media_files]

file OUTPUT_HTML_FILE => [OUTPUT_HTML_DIR, DOCUMENT_MAIN_FILE, *INCLUDED_FILES] do |t|
sh "asciidoctor", "--backend", "html5", DOCUMENT_MAIN_FILE, "-o", t.name
end

file :html_media_files => OUTPUT_HTML_DIR do |t|
MEDIA_FILES.each do |source|
relative_part = source.dup
relative_part.slice! CONTENT_DIR
destination = File.join(OUTPUT_HTML_DIR, relative_part)
destination_dir = File.dirname(destination)

if File.exist? destination
next if [File.ctime(source), File.mtime(source)].max <= \
[File.ctime(destination), File.mtime(destination)].max
end
mkdir_p destination_dir
cp_r source, destination
end
end

directory OUTPUT_HTML_DIR

desc 'Generar la versión en PDF de la documentación'
task :pdf => OUTPUT_PDF_FILE

file OUTPUT_PDF_FILE => [OUTPUT_PDF_DIR, DOCUMENT_MAIN_FILE, *INCLUDED_FILES] do |t|
sh "asciidoctor", "--require", "asciidoctor-pdf", "--backend", "pdf", DOCUMENT_MAIN_FILE, "-o", t.name
end

directory OUTPUT_PDF_DIR

desc 'Generar la versión en EPUB de la documentación'
task :epub => OUTPUT_EPUB_FILE

file OUTPUT_EPUB_FILE => [OUTPUT_EPUB_DIR, DOCUMENT_MAIN_FILE, *INCLUDED_FILES] do |t|
sh "asciidoctor", "--require", "asciidoctor-epub3", "--backend", "epub3", DOCUMENT_MAIN_FILE, "-o", t.name
end

directory OUTPUT_EPUB_DIR

desc 'Ejecutar los tests'
task :tests => [:test_missing_variables, :test_htmlproofer]

desc 'Test de HTMLProofer'
task :test_htmlproofer => :html do |t|
typhoeus_config = {
:headers => {
"User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0",
},
:ssl_verifyhost => 0,
:ssl_verifypeer => false,
}
sh "htmlproofer", "--typhoeus_config", typhoeus_config.to_json, OUTPUT_HTML_DIR
end

desc 'Test de variables no definidas'
task :test_missing_variables => OUTPUT_HTML_FILE do |t|
document = Nokogiri::HTML.parse(open(OUTPUT_HTML_FILE))
variables = document.xpath('//body//text()[not(ancestor::div[@class="stemblock"])]').flat_map do |text|
matches = text.content.gsub(/\\\$.*?\\\$/, "").scan(/\{\w+\}/)
end.uniq.sort

fail "Se han encontrado #{variables.size} variables no definidas:\n#{variables.join("\n")}" unless variables.empty?
puts variables
end

desc 'Limpiar todos los archivos generados'
task :clean do |t|
rm_r OUTPUT_DIR
end

desc 'Generar el archivo de información estadística'
task :docstats do |t|
# Contamos las palabras en la versión HTML del documento
output_html = `asciidoctor --backend html5 #{DOCUMENT_MAIN_FILE} -o -`
docstats = open(DOCSTATS_FILE, "w")

document = Nokogiri::HTML.parse(output_html)
document.css(".sect1 > h2 + .sectionbody").each do |sectionbody|
if matches = /^(?<chapter_number>\d+)\. /.match(sectionbody.previous_element)
chapter_number = matches['chapter_number']

wordcount = sectionbody.text().scan(/[\p{Alnum}\-']+/).length()
reading_time = (wordcount.to_f / READING_SPEED_IN_WORDS_PER_MINUTE).round

# Construir la cadena de texto con el tiempo de lectura
if reading_time == 1
reading_time_string = "1 minuto"
elsif reading_time < 60
reading_time_string = "#{reading_time} minutos"
elsif reading_time == 60
reading_time_string = "1 hora"
else
reading_time_string = "#{reading_time / 60} horas y #{reading_time % 60} minutos"
end

docstats.puts ":C%02i_words: %i" % [chapter_number, wordcount]
docstats.puts ":C%02i_reading_time: %s" % [chapter_number, reading_time_string]

puts "C%02i: %8i %8i (%s)" % [chapter_number, wordcount, reading_time, reading_time_string]
end
end
ensure
docstats.close
end
task :config do |t|
CONFIG[:asciidoctor_pdf_opts] = [
'--require', 'asciidoctor-mathematical', '-a', 'mathematical-format=svg',
]
end
6 changes: 5 additions & 1 deletion config/attributes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ include::locale/attributes.adoc[]
:experimental:
// Incluir la tabla de contenidos.
:toc: left
// Separador de miles
:K: &#x2009;
// Configuración del backend HTML5
ifdef::backend-html5[include::html5/attributes.adoc[]]
ifdef::backend-html5[include::html5/attributes.adoc[]]
// Configuración del backend PDF
ifdef::backend-pdf[include::pdf/attributes.adoc[]]
4 changes: 2 additions & 2 deletions config/html5/attributes.adoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Archivo de configuración específica del backend HTML5.
//
// Localización de los archivos docinfo
:docinfodir: ../config/html5
// Localización de los archivos docinfo.
:docinfodir: {basedir}/config/html5
:docinfo: shared-head
1 change: 1 addition & 0 deletions config/locale/attributes-es.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ ifdef::preface-title[:preface-title: Prefacio]
:toc-title: Tabla de Contenido
:untitled-label: Sin título
:version-label: Versión
:video-caption: Vídeo
:warning-caption: Aviso
6 changes: 6 additions & 0 deletions config/pdf/attributes.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Archivo de configuración específica del backed PDF.
//
// Archivo de tema para asciidoctor-pdf
:pdf-theme: config/pdf/pdf-theme.yml
// Activar la portada
:title-page:
17 changes: 17 additions & 0 deletions config/pdf/pdf-theme.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
extends: default
title-page:
authors:
content:
name_only: "{author}"
with_email: "{author} <{email}>"
with_url: "{url}[{author}]"
image:
align: center
caption:
align: inherit
admonition:
icon:
time:
name: fa-clock
stroke_color: 871452
size: 24
4 changes: 2 additions & 2 deletions content/main.adoc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
= Sistemas Operativos
Jesús Torres <jmtorres@ull.es>
Curso 20XX-20XY
:ejemplos_branch: master
include::version.adoc[]
Curso {academic-year}
include::../config/attributes.adoc[]
include::enlaces.adoc[]
include::docstats.adoc[]
Expand Down
2 changes: 2 additions & 0 deletions content/version.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:academic-year: 20XX-20XY
:ejemplos_branch: master
20 changes: 20 additions & 0 deletions lib/task_helpers/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Struct.new('Config',
:asciidoctor_opts,
:asciidoctor_html_opts,
:asciidoctor_epub_opts,
:asciidoctor_pdf_opts,
:htmlproofer_opts,
keyword_init: true) do
def initialize()
default_values = {
asciidoctor_opts: [],
asciidoctor_html_opts: [],
asciidoctor_epub_opts: [],
asciidoctor_pdf_opts: [],
htmlproofer_opts: ENV.key?('HTMLPROOFER_DISABLE_EXTERNAL') ? ["--disable-external"] : []
}
super(default_values)
end
end

CONFIG = Struct::Config.new()
Loading

0 comments on commit 7cc8c75

Please sign in to comment.