From 7569cc07824b7653faf4841974d1eabe904177e2 Mon Sep 17 00:00:00 2001 From: Michael Beck Date: Wed, 4 Dec 2024 17:21:40 +0100 Subject: [PATCH] adds template and makefiles --- .gitignore | 1 + README.md | 17 + ResearchProposal.md | 102 ++ apa-7th-edition.csl | 1917 +++++++++++++++++++++++++++++++++ citation-abbreviations.csl | 8 + filters/.gitkeep | 0 filters/first-line-indent.lua | 592 ++++++++++ lit.bib | 0 make.sh | 25 + modify-pdf.sh | 37 + resources/.gitkeep | 0 11 files changed, 2699 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 ResearchProposal.md create mode 100644 apa-7th-edition.csl create mode 100644 citation-abbreviations.csl create mode 100644 filters/.gitkeep create mode 100644 filters/first-line-indent.lua create mode 100644 lit.bib create mode 100755 make.sh create mode 100755 modify-pdf.sh create mode 100644 resources/.gitkeep diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a136337 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pdf diff --git a/README.md b/README.md new file mode 100644 index 0000000..8f2fc31 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +## Requirements + +- pdftk +- ghostscript + +### Install: + +#### Debian +```bash +sudo apt install pdftk gs +``` + +#### Arch + +```bash +yay -S pdftk ghostscript +``` diff --git a/ResearchProposal.md b/ResearchProposal.md new file mode 100644 index 0000000..f7ea0cd --- /dev/null +++ b/ResearchProposal.md @@ -0,0 +1,102 @@ +--- +documentclass: article + +author: +- Michael Beck + +title: "Refuge-Seeking Migration and Crime" +description: "Research Proposal" +subtitle: "Disentangling the Effect of Diverse Refuge-Seeking Migration on Hate-Crime in the Light of the Ukrainian Displacement Following the Russian Invasion." +date: "2024-08-08" +lang: en-US + +toc: true +toc-depth: 3 +# toccolor: gray +number-sections: true + +color-links: true +urlcolor: blue +linkcolor: blue +link-citations: true + +lot: true +lof: true + +bibliography: lit.bib + +mainfont: Latin Modern Roman +fontfamilyoptions: lmodernscellL +geometry: "left=2.5cm,right=3cm,top=2.5cm,bottom=2.5cm" + +header-includes: | + \usepackage{pdflscape} + \newcommand{\blandscape}{\begin{landscape}} + \newcommand{\elandscape}{\end{landscape}} + \setcounter{page}{-1} + +include-before: | + \newcommand{\scellL}[1]{% + \parbox{4.2cm}{\raggedright\leftskip=1em\hskip-1em#1} + } + \newcommand{\scell}[1]{% + \parbox{3cm}{ + \begin{center}#1 + \end{center} + \vspace{1mm} + } + } + \newcommand{\scellB}[1]{% + \parbox{2.5cm}{ + \begin{center}#1 + \end{center} + \vspace{1mm} + } + } + \begin{center} + \vfill + \textbf{Research Proposal} \\ + Seminar Comparative Research in Crime and Delinquency \\ + Dr. Alexander Trinidad \\ + \vspace{1cm} + \hrule + \end{center} + Matriculation number: \textbf{7406366} + \thispagestyle{empty} + \newpage + \thispagestyle{empty} + +--- + +\vfill + +\newpage + +# Research Question + +# Motivation + +# Data + +# Method + + +\newpage + +# References + +::: {#refs} +::: + +\newpage + +# Eigenständigkeitserklärung + +Hiermit versichere ich, dass ich die vorliegende Arbeit selbstständig und ohne die Benutzung anderer als der angegebenen Hilfsmittel angefertigt habe. Alle Stellen, die wörtlich oder sinngemäß aus veröffentlichten und nicht veröffentlichten Schriften entnommen wurden, sind als solche kenntlich gemacht. + +\vspace{5cm} + +\begin{tabular}{@{}p{4in}@{}} + \hrulefill \\ + Michael Beck, 08.08.2024 \\ +\end{tabular} \ No newline at end of file diff --git a/apa-7th-edition.csl b/apa-7th-edition.csl new file mode 100644 index 0000000..e8cd9ae --- /dev/null +++ b/apa-7th-edition.csl @@ -0,0 +1,1917 @@ + + diff --git a/citation-abbreviations.csl b/citation-abbreviations.csl new file mode 100644 index 0000000..3d6fb45 --- /dev/null +++ b/citation-abbreviations.csl @@ -0,0 +1,8 @@ +{ "default": { + "container-title": { + "European Social Survey European Research Infrastructure": "ESS ERIC", + "Bundeskriminalamt": "BKA", + "Scots Law Times": "SLT" + } + } +} \ No newline at end of file diff --git a/filters/.gitkeep b/filters/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/filters/first-line-indent.lua b/filters/first-line-indent.lua new file mode 100644 index 0000000..52b0f66 --- /dev/null +++ b/filters/first-line-indent.lua @@ -0,0 +1,592 @@ +--[[# first-line-indent.lua – First line indentation filter + +Copyright: © 2021–2023 Contributors +License: MIT – see LICENSE for details + +@TODO latex_quote should use options.size (or better, a specific option) +@TODO option for leaving indents after headings (French style) +@TODO smart setting of the post-heading style based on `lang` +@TODO option to leave indent at the beginning of the document + +]] + +PANDOC_VERSION:must_be_at_least '2.17' +stringify = pandoc.utils.stringify +equals = pandoc.utils.equals +pandoctype = pandoc.utils.type + +-- # Options + +---@class Options Options map with default values. +---@field format string|nil output format (currently: 'html' or 'latex') +---@field indent boolean whether to use first line indentation globally +---@field set_metadata_variable boolean whether to set the `indent` +-- metadata variable. +---@field set_header_includes boolean whether to provide formatting code in +-- header-includes. +---@field auto_remove_indents boolean whether to automatically remove +-- indents after specific block types. +---@field remove_after table list of strings, Pandoc AST block types +-- after which first-line indents should be automatically removed. +---@field remove_after_class table list of strings, classes of elements +-- after which first-line indents should be automatically removed. +---@field dont_remove_after_class table list of strings, classes of elements +-- after which first-line indents should not be removed. Prevails +-- over remove_after. +---@field size string|nil a CSS / LaTeX specification of the first line +-- indent length +---@field recursive table Pandoc Block types to +--- which the filter is recursively applied, with options map. +--- The option `dont_indent_first` controls whether indentation +--- is removed on the first paragraph. +local Options = { + format = nil, + indent = true, + set_metadata_variable = true, + set_header_includes = true, + auto_remove = true, + remove_after = pandoc.List({ + 'BlockQuote', + 'BulletList', + 'CodeBlock', + 'DefinitionList', + 'HorizontalRule', + 'OrderedList', + }), + remove_after_class = pandoc.List({ + 'statement', + }), + dont_remove_after_class = pandoc.List:new(), + size = nil, -- default let LaTeX decide + size_default = '1.5em', -- default value for HTML + recursive = { + Div = {dont_indent_first = false}, + BlockQuote = {dont_indent_first = true}, + } +} + +-- # Filter global variables + +---@class code map pandoc objects for indent/noindent Raw code. +local code = { + tex = { + indent = pandoc.RawInline('tex', '\\indent '), + noindent = pandoc.RawInline('tex', '\\noindent '), + }, + latex = { + indent = pandoc.RawInline('latex', '\\indent '), + noindent = pandoc.RawInline('latex', '\\noindent '), + }, + html = { + indent = pandoc.RawBlock('html', + '
'), + noindent = pandoc.RawBlock('html', + '
'), + } +} + + +---LATEX_QUOTE_ENV: LaTeX's definition of the quote environement +---used to define HeaderIncludes. +---a \setlength{\parindent}{} will be appended +---@type string +local LATEX_QUOTE_ENV = [[\makeatletter + \renewenvironment{quote} + {\list{}{\listparindent 1.5em% + \itemindent \listparindent + \rightmargin \leftmargin + \parsep \z@ \@plus \p@}% + \item\noindent\relax} + {\endlist} + \makeatother +]] + +---@class HeaderIncludes map of functions to produce +---header-includes code given a size parameter (string|nil), +--- either for global or for local indentation markup. +--- optionally wrap the constructed global header markup (e.g. " + end, + latex = function(str) return str end, + }, + loc = { + html = function(size) + size = size or Options.size_default + local code = [[ div.no-first-line-indent-after + p { + text-indent: 0; + } + div.first-line-indent-after + p { + text-indent: SIZE; + } +]] + return code:gsub("SIZE", size) + end, + latex = function(_) return '' end, + } +} + +-- # encapsulate Quarto/Pandoc variants + +---format_match: whether format matches a string pattern +---ex: format_match('html5'), format_match('html*') +---in Quarto we try removing non-alphabetical chars +---@param pattern string +---@return boolean +local function format_match(pattern) + return quarto and (quarto.doc.is_format(pattern) + or quarto.doc.is_format(pattern:gsub('%A','')) + ) + or FORMAT:match(pattern) +end + +---add_header_includes: add a block to the document's header-includes +---meta-data field. +---@param meta pandoc.Meta the document's metadata block +---@param blocks pandoc.Blocks list of Pandoc block elements (e.g. RawBlock or Para) +--- to be added to the header-includes of meta +---@return pandoc.Meta meta the modified metadata block +local function add_header_includes(meta, blocks) + + -- Pandoc + local function pandoc_add_headinc(meta,blocks) + + local header_includes = pandoc.MetaList( { pandoc.MetaBlocks(blocks) }) + + -- add any exisiting meta['header-includes'] + -- it can be MetaInlines, MetaBlocks or MetaList + if meta['header-includes'] then + if pandoctype(meta['header-includes']) == 'List' then + header_includes:extend(meta['header-includes']) + else + header_includes:insert(meta['header-includes']) + end + end + + meta['header-includes'] = header_includes + + return meta + + end + + -- Quarto + local function quarto_add_headinc(blocks) + quarto.doc.include_text('in-header', stringify(blocks)) + end + + return quarto and quarto_add_headinc(blocks) + or pandoc_add_headinc(meta,blocks) + +end + + +-- # Helper functions + +-- ensure_list: turns Inlines and Blocks meta values into list +local function ensure_list(elem) + if elem and (pandoctype(elem) == 'Inlines' + or pandoctype(elem) == 'Blocks') then + elem = pandoc.List:new(elem) + end + return elem +end + + +--- classes_include: check if one of an element's class is in a given +-- list. Returns true if match, nil if no match or the element doesn't +-- have classes. +---@param elem table pandoc AST element +---@param classes table pandoc List of strings +local function classes_include(elem,classes) + + if elem.classes then + + for _,class in ipairs(classes) do + if elem.classes:includes(class) then return true end + end + + end + +end + +--- is_indent_cmd: check if an element is a LaTeX indent command +---@param elem pandoc.Inline +---@return string|nil 'indent', 'noindent' or nil +-- local function is_indent_cmd(elem) +-- return (equals(elem, code.latex.indent) +-- or equals(elem, code.tex.indent)) and 'indent' +-- or (equals(elem, code.latex.noindent) +-- or equals(elem, code.tex.noindent)) and 'noindent' +-- or nil +-- end +local function is_indent_cmd(elem) + return elem.text and ( + elem.text:match('^%s*\\indent%s*$') and 'indent' + or elem.text:match('^%s*\\noindent%s*$') and 'noindent' + ) + or nil +end + +-- # Filter functions + +--- Add format-specific explicit indent markup to a paragraph. +--- Returns a list of blocks containing a single paragraph +--- or a rawblock followed by a paragraph, depending on format. +---@param type string 'indent' or 'noindent', type of markup to add +---@param elem pandoc.Para +---@return pandoc.Blocks +local function indent_markup(type, elem) + local result = pandoc.List:new() + + if not (type == 'indent' or type == 'noindent') then + + result:insert(elem) + + elseif format_match('latex') then + + -- in LaTeX, replace any `\indent` or `\noindent` + -- at the beginning of the paragraph with + -- with the one corresponding to `type` + + if elem.content[1] and is_indent_cmd(elem.content[1]) then + elem.content[1] = code.tex[type] + else + elem.content:insert(1, code.tex[type]) + end + result:insert(elem) + + + elseif format_match('html') then + + result:extend({ code.html[type], elem }) + + end + + return result + +end + +--- process_blocks: process indentations in a list of blocks. +-- Adds output code for explicitly specified first-line indents, +-- automatically removes first-line indents after blocks of the +-- designed types unless otherwise specified. +---@param blocks pandoc.Blocks element (list of blocks) +---@param dont_indent_first boolean whether to indent the first paragraph +local function process_blocks(blocks, dont_indent_first) + dont_indent_first = dont_indent_first or false + -- tag for the first element + local is_first_block = true -- tags the doc's first element + -- tag to trigger indentation auto-removal on the next element + local dont_indent_next_block = false + local result = pandoc.List:new() + + for _,elem in pairs(blocks) do + + -- Paragraphs: if they have explicit LaTeX indent markup + -- reproduce it in the output format, otherwise + -- remove indentation if needed, provided `auto_remove` is on. + if elem.t == "Para" then + + if elem.content[1] and is_indent_cmd(elem.content[1]) then + + -- 'indent' or 'noindent' ? + local type = is_indent_cmd(elem.content[1]) + + result:extend(indent_markup(type, elem)) + + elseif is_first_block and dont_indent_first then + + result:extend(indent_markup('noindent', elem)) + + elseif dont_indent_next_block and Options.auto_remove then + + result:extend(indent_markup('noindent', elem)) + + else + + result:insert(elem) + + end + + dont_indent_next_block = false + + -- Non-Paragraphs: check first whether it's an element after + -- which indentation must be removed. Next insert it, applying + -- this function recursively within the element if needed. + else + + if Options.auto_remove then + + if Options.remove_after:includes(elem.t) and + not classes_include(elem, Options.dont_remove_after_class) then + + dont_indent_next_block = true + + elseif elem.classes and + classes_include(elem, Options.remove_after_class) then + + dont_indent_next_block = true + + else + + dont_indent_next_block = false + + end + + end + + -- recursively process the element if needed + if Options.recursive[elem.t] then + + local dif = Options.recursive[elem.t].dont_indent_first + elem.content = process_blocks(elem.content, dif) + + end + + -- insert + result:insert(elem) + + end + + -- ensure `is_first_block` turns to false + -- even if the first block wasn't a paragraph + -- or if it had explicit indent marking + is_first_block = false + + end + + return result + +end + +--- process_doc: Process indents in the document's body text. +-- Adds output code for explicitly specified first-line indents, +-- automatically removes first-line indents after blocks of the +-- designed types unless otherwise specified. +local function process_doc(doc) + local dont_indent_first = false + + -- if no output format, do nothing + if not Options.format then return end + + -- if the doc has a title, do not indent first paragraph + if doc.meta.title then + dont_indent_first = true + end + + doc.blocks = process_blocks(doc.blocks, dont_indent_first) + + return doc + +end + + +--- read_user_options: read user options from meta element. +-- in Quarto options may be under format/pdf or format/html +-- the latter override root ones. +local function read_user_options(meta) + local user_options = {} + + if meta.indent == false then + Options.indent = false + end + + if meta['first-line-indent'] then + user_options = meta['first-line-indent'] + end + + local formats = {'pdf', 'html', 'latex'} + if meta.format then + for format in ipairs(formats) do + if format_match(format) and meta.format[format] then + for k,v in meta.format[format] do + user_options[k] = v + end + end + end + end + + if user_options['set-metadata-variable'] == false then + Options.set_metadata_variable = false + end + + if user_options['set-header-includes'] == false then + Options.set_header_includes = false + end + + -- size + -- @todo using stringify means that LaTeX commands in + -- size are erased. But it ensures that the filter gets + -- a string. Improvement: check that we have a string + -- and throw a warning otherwise + if user_options.size and pandoctype(user_options.size == 'Inlines') then + + Options.size = stringify(user_options.size) + + end + + if user_options['auto-remove'] == false then + Options.auto_remove = false + end + + -- autoremove elements and classes + -- for elements we only need a whitelist, remove_after + -- for classes we need both a whitelist (remove_after_class) + -- and a blacklist (dont_remove_after_class). + + -- first insert user values in `remove_after`, `remove_after_class` + -- and `dont_remove_after_class`. + for optname, metakey in pairs({ + remove_after = 'remove-after', + remove_after_class = 'remove-after-class', + dont_remove_after_class = 'dont-remove-after-class', + }) do + + local user_value = ensure_list(user_options[metakey]) + + if user_value and pandoctype(user_value) == 'List' then + + for _,item in ipairs(user_value) do + + Options[optname]:insert(stringify(item)) + + end + + end + + end + + -- then remove blacklisted entries from `remove_after` + -- and `remove_after_class`. + for optname, metakey in pairs({ + remove_after = 'dont-remove-after', + remove_after_class = 'dont-remove-after-class' + }) do + + local user_value = ensure_list(user_options[metakey]) + + if user_value and pandoctype(user_value) == 'List' then + + -- stringify the list + for i,v in ipairs(user_value) do + user_value[i] = stringify(v) + end + + -- filter to that returns true iff an item isn't blacklisted + local predicate = function (str) + return not(user_value:includes(str)) + end + + -- apply the filter to the whitelist + Options[optname] = Options[optname]:filter(predicate) + + end + + end + +end + +--- set_meta: insert options in doc's meta +--- Sets `indent` and extends `header-includes` if needed. +---@param meta pandoc.Meta +---@return pandoc.Meta|nil meta nil if no changes +local function set_meta(meta) + local changes = false -- only return if changes are made + local header_code = nil + local format = Options.format + + -- set the `indent` metadata variable unless otherwise specified or + -- already set to false + if Options.set_metadata_variable and not(meta.indent == false) then + meta.indent = true + changes = true + end + + -- set the `header-includes` metadata variable + if Options.set_header_includes and Options.indent then + + -- do we apply first line indentation globally? + if Options.indent then + header_code = HeaderIncludes.glob[format](Options.size) + end + -- provide local explicit indentation styles + header_code = header_code .. HeaderIncludes.loc[format](Options.size) + -- wrap the header if needed + header_code = HeaderIncludes.wrap[format](header_code) + + -- insert if not empty + if header_code ~= '' then + add_header_includes(meta, { pandoc.RawBlock(format, header_code)}) + changes = true + end + + end + + return changes and meta or nil + +end + +--- process_metadata: process user options. +-- read user options, set the `indent` metadata variable, +-- add formatting code to `header-includes`. +local function process_metadata(meta) + local changes = false -- only return if changes are made + local header_code = nil + local format = format_match('html') and 'html' + or (format_match('latex') and 'latex') + + if not format then + return nil + else + Options.format = format + end + + read_user_options(meta) -- places values in global `options` + + return set_meta(meta) + +end + +--- Main code +-- Returns the filters in the desired order of execution +return { + { + Meta = process_metadata + }, + { + Pandoc = process_doc + } +} diff --git a/lit.bib b/lit.bib new file mode 100644 index 0000000..e69de29 diff --git a/make.sh b/make.sh new file mode 100755 index 0000000..88ade72 --- /dev/null +++ b/make.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +FILENAME="ResearchProposal" + +IN="${FILENAME}.md" +OUT="${FILENAME}.pdf" + +# Generate the PDF from Markdown +echo "Generating PDF..." +pandoc -i "$IN" \ + -o "$OUT" \ + --csl=apa-7th-edition.csl \ + --citeproc \ + --lua-filter=filters/first-line-indent.lua \ + --citation-abbreviations=citation-abbreviations.csl + +# Check if pandoc ran successfully +if [ $? -ne 0 ]; then + echo "Error: pandoc command failed. Exiting script." + exit 1 +fi + +# Insert Erklärung.pdf at the end of the PDF +echo "Modifying the PDF..." +./modify-pdf.sh "$OUT" "Erklärung.pdf" "$OUT" diff --git a/modify-pdf.sh b/modify-pdf.sh new file mode 100755 index 0000000..34788a7 --- /dev/null +++ b/modify-pdf.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +# Check if pdftk and ghostscript are installed +if ! command -v pdftk &>/dev/null || ! command -v gs &>/dev/null; then + echo "Error: pdftk and ghostscript are required but not installed." + exit 1 +fi + +# Check for input arguments +if [ "$#" -ne 3 ]; then + echo "Replaces the last page of a pdf file (input) with another (insert) and saves the file (output)." + echo "Usage: $0 input.pdf insert.pdf output.pdf" + exit 1 +fi + +input_pdf="$1" +insert_pdf="$2" +output_pdf="$3" + +# Get the number of pages in the input PDF +num_pages=$(pdftk "$input_pdf" dump_data | grep NumberOfPages | awk '{print $2}') + +if [ -z "$num_pages" ]; then + echo "Error: Unable to determine the number of pages in the input PDF." + exit 1 +fi + +# Extract all pages except the last one +pdftk "$input_pdf" cat 1-$(($num_pages - 1)) output temp_removed_last.pdf + +# Merge the modified PDF with the insert PDF +pdftk temp_removed_last.pdf "$insert_pdf" cat output "$output_pdf" + +# Clean up temporary files +rm -f temp_removed_last.pdf + +echo "Successfully created $output_pdf by replacing the last page with $insert_pdf." diff --git a/resources/.gitkeep b/resources/.gitkeep new file mode 100644 index 0000000..e69de29