Skip to content

Commit

Permalink
feat(templates/typst-report): init
Browse files Browse the repository at this point in the history
  • Loading branch information
dixslyf committed Sep 10, 2024
1 parent b9729e0 commit 0f88d63
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 0 deletions.
4 changes: 4 additions & 0 deletions templates/flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ _:
path = ./java-maven;
description = "A Java and Maven project";
};
typst-report = {
path = ./typst-report;
description = "A report written in Typst";
};
};
}
1 change: 1 addition & 0 deletions templates/typst-report/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake .
2 changes: 2 additions & 0 deletions templates/typst-report/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.direnv
*.pdf
12 changes: 12 additions & 0 deletions templates/typst-report/doc/constants.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#let TEXT_FONT = "Linux Libertine"
#let TEXT_SIZE = 12pt
#let MATH_SIZE = 12pt
#let TITLE_SIZE = TEXT_SIZE * 1.4
#let SUBTITLE_SIZE = TEXT_SIZE
#let HEADING_SIZE = TEXT_SIZE * 1.3
#let SUBHEADING_SIZE = TEXT_SIZE * 1.2
#let SUBSUBHEADING_SIZE = TEXT_SIZE
#let LIST_INDENT = 0.5cm
#let OUTLINE_INDENT = 1em
#let CODE_BLOCK_BG = rgb("#F8F9F9")
#let CODE_TEXT_SIZE = 10pt
125 changes: 125 additions & 0 deletions templates/typst-report/doc/doc.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#import "/constants.typ": *
#import "/lib.typ": *

// SETUP

#set page(
paper: "a4",
numbering: "1",
number-align: right + top
)

#set heading(numbering: "1.")

#show heading.where(level: 1): it => [
#set text(size: HEADING_SIZE, weight: "bold")
#it
]

#show heading.where(level: 2): it => [
#set text(size: SUBHEADING_SIZE, weight: "regular", style: "italic")
#it
]

#show heading.where(level: 3): it => [
#set text(size: SUBSUBHEADING_SIZE, weight: "regular", style: "normal")
#it
]

#show outline: it => {
set par(leading: 1em)
it
}

#set figure(placement: none)

#set block(above: 1.5em)

#set par(justify: true)

#set text(size: TEXT_SIZE, font: TEXT_FONT)

#set list(indent: LIST_INDENT)

#set terms(indent: LIST_INDENT)

#show math.equation: set text(size: MATH_SIZE)

#show link: it => {
set text(fill: blue)
underline(it)
}

#set list(indent: LIST_INDENT)
#set enum(indent: LIST_INDENT)

#show figure: set block(breakable: false)

#show table.cell.where(y: 0): set text(weight: "bold")
#set table(
align: (x, y) => if y == 0 { center + horizon } else { center + top },
stroke: (x, y) => (
top: if y <= 1 { 0.8pt } else { 0pt },
bottom: 1pt,
),
inset: 0.5em,
)

// CONTENT

#let date = datetime(
year: 1970,
month: 1,
day: 1,
)

#align(center, [
#text(size: TITLE_SIZE)[*Title*] \
\
#text(size: SUBTITLE_SIZE)[
Subtitle \
#date.display("[day] [month repr:long], [year]")
]
])

#outline(indent: auto)

#stick-together(
[= Section 1],
[#lorem(30) @example-1970]
)

#figure(
caption: lorem(5),
table(
columns: 3,
table.header[Header 1][Header 2][#lorem(16)],
[1], [2], [3], [4], [5], lorem(32)
),
)

#stick-together(
[== Subsection],
[
#display-question[What is $1 + 1$?]

#lorem(30)
]
)

#stick-together(
threshold: 10em,
[=== Subsubsection],
[
#display-code(lang: "c", "#include <stdio.h>

int main() {
printf(\"Hello, world!\\n\");
return 0;
}")

#lorem(30)
]
)

#bibliography("/references.bib")
42 changes: 42 additions & 0 deletions templates/typst-report/doc/lib.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#import "/constants.typ": *
#import "/lib/stick-together.typ": stick-together

#let display-code(content, lang: none, inset: 1em, breakable: false) = align(center, {
set text(size: CODE_TEXT_SIZE)

show raw.where(block: true): block.with(
fill: CODE_BLOCK_BG,
inset: inset,
breakable: breakable
)

raw(
lang: lang,
block: true,
content
)
})

#let display-question(content, heading-size: TEXT_SIZE) = align(
center,
context {
let heading = text(size: heading-size, weight: "bold", "Question:")
let size = measure(heading)
block(
breakable: true,
fill: CODE_BLOCK_BG,
inset: (
top: 20pt - (heading-size - size.height),
right: 20pt,
bottom: 20pt,
left: 20pt
),
width: 100%,
align(
start,
stick-together(threshold: 10em, heading, content)
)
)
}
)

34 changes: 34 additions & 0 deletions templates/typst-report/doc/lib/stick-together.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// MIT No Attribution
//
// Copyright (c) 2023 Pg Biel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify,
// merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// Attempts to ensure the two elements will be in the same page.
// The threshold specifies how early a pagebreak should be forced.
// That is, the minimum vertical space that both elements should
// have available in the same page; if they have less than that
// threshold, they are moved to the next page.
// A higher threshold means that a pagebreak is forced earlier.
// When using with tables, a threshold of at least the height
// of the heading + the header row + a little bit should be used
// to ensure the header rows won't be alone.
// E.g. 5em could be good for a small table, but you will have to fiddle
// with it (make sure to test it by pushing your table to the bottom and seeing
// how early the elements are moved to the next page).
#let stick-together(a, b, threshold: 3em) = {
block(a + v(threshold), breakable: false)
v(-1 * threshold)
b
}
10 changes: 10 additions & 0 deletions templates/typst-report/doc/references.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@article{example-1970,
title = {Lorem Ipsum},
url = {https://www.example.com/},
urldate = {1970-01-01},
journal = {Example Journal},
author = {Doe, John and Doe, Jane},
month = {01},
year = {1970},
pages = {1--999},
}
18 changes: 18 additions & 0 deletions templates/typst-report/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
inputs = {
flake-utils.url = "github:Numtide/flake-utils";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};

outputs = { flake-utils, nixpkgs, ... }: flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
{
devShells.default = pkgs.mkShell {
packages = with pkgs; [
typst
];
};
});
}

0 comments on commit 0f88d63

Please sign in to comment.