Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/preview/typsium-ghs/0.1.1/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2025

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, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

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.
44 changes: 44 additions & 0 deletions packages/preview/typsium-ghs/0.1.1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[![Typst Package](https://img.shields.io/badge/dynamic/toml?url=https%3A%2F%2Fraw.githubusercontent.com%2FTypsium%2Ftypsium-ghs%2Fmain%2Ftypst.toml&query=%24.package.version&prefix=v&logo=typst&label=package&color=239DAD)](https://github.com/Typsium/typsium-ghs)
[![MIT License](https://img.shields.io/badge/license-MIT-blue)](https://github.com/Typsium/typsium/blob/main/LICENSE)
![User Manual](https://img.shields.io/badge/manual-.pdf-purple)

# Typst GHS and HP Statements

A Typst package for quickly displaying and formatting Hazard and Precautionary statements.

## Usage
Use the `#ghs` command to display a GHS pictogram, to get more information about each pictogram use `#ghs-info`
```typst
#import "@preview/typsium-ghs:0.1.0": *

Check warning on line 12 in packages/preview/typsium-ghs/0.1.1/README.md

View check run for this annotation

Typst package check / @preview/typsium-ghs:0.1.1

packages/preview/typsium-ghs/0.1.1/README.md#L12

This import seems to use an older version of the package.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please update the import here?

#grid(
columns: 9,
..range(1,10).map(x=> ghs(x))
)
```
![result](https://raw.githubusercontent.com/Typsium/typsium-ghs/main/tests/ghs-pictograms/ref/1.png)

Easily Display Hazard and Precautionary statements in three languages (english, french and german):
```typst
#h-statement(310)\
#p-statement(310)\
#hp("P305+P351+P338", only-statement:true)
```
![result](https://raw.githubusercontent.com/Typsium/typsium-ghs/main/tests/simple-hp/ref/1.png)

There are variants for some statements:
```typst
#p-statement(310, only-statement: true)\
#p-statement(310, variant:1)\
#p-statement(310, variant:2)
```
![result](https://raw.githubusercontent.com/Typsium/typsium-ghs/main/tests/variants/ref/1.png)

You can also add parameters when needed:
```typst
#h-statement(370)\
#h-statement(370, parameters:"the lungs")\
#h-statement(370, parameters:("the lungs", "when inhaled"))\
#h-statement(370, variant:1)\
#h-statement(370, variant:1, parameters:"when inhaled")\
```
![result](https://raw.githubusercontent.com/Typsium/typsium-ghs/main/tests/parameters/ref/1.png)
31 changes: 31 additions & 0 deletions packages/preview/typsium-ghs/0.1.1/src/ghs.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#let validate-index(index)={
if type(index) == str{
return int(index.find(regex("\d+")))
}else if type(index) == int{
return index
}
return 1
}

#let ghs-info=(
GHS01:(name:"Explosive",image:"resources/ghs-pictograms/GHS01.svg", usage:("Unstable explosives", "Explosives", "Self-reactive substances and mixtures", "Organic peroxides")),
GHS02:(name:"Flammable",image:"resources/ghs-pictograms/GHS02.svg", usage: ("Flammable gases", "Flammable aerosols", "Flammable liquids", "Flammable solids", "Self-reactive substances and mixtures", "Pyrophoric liquids", "Pyrophoric solids", "Combustible solids", "Combustible liquids", "Self-heating substances and mixtures", "Substances and mixtures, which in contact with water, emit flammable gases", "Organic peroxides")),
GHS03:(name:"Oxidizing",image:"resources/ghs-pictograms/GHS03.svg", usage:("Oxidizing gases", "Oxidizing liquids", "Oxidizing solids")),
GHS04:(name:"Compressed Gas",image:"resources/ghs-pictograms/GHS04.svg", usage:("Compressed gases", "Liquefied gases", "Refrigerated liquefied gases", "Dissolved gases")),
GHS05:(name:"Corrosive",image:"resources/ghs-pictograms/GHS05.svg", usage:("Corrosive to metals", "Skin corrosion", "Serious eye damage")),
GHS06:(name:"Toxic",image:"resources/ghs-pictograms/GHS06.svg", usage:("Acute toxicity (oral, dermal, inhalation)",)),
GHS07:(name:"Health Hazard",image:"resources/ghs-pictograms/GHS07.svg", usage:("Acute toxicity (oral, dermal, inhalation)", "Skin irritation", "Eye irritation", "Skin sensitization", "Specific target organ toxicity following single exposure", "Respiratory tract irritation", "Narcotic effects")),
GHS08:(name:"Serious Health hazard",image:"resources/ghs-pictograms/GHS08.svg", usage:("Respiratory sensitization", "Germ cell mutagenicity", "Carcinogenicity", "Reproductive toxicity", "Specific target organ toxicity following single exposure", "Specific target organ toxicity following repeated exposure", "Aspiration hazard")),
GHS09:(name:"Toxic",image:"resources/ghs-pictograms/GHS09.svg", usage:("Acute hazards to the aquatic environment", "Chronic hazards to the aquatic environment")),
)

#let ghs(
icon,
width:auto,
height:auto,
fit: "cover",
)={
let index = calc.clamp(validate-index(icon), 1, 9)

image("resources/ghs-pictograms/GHS0" + str(index) + ".svg", height: height, width: width, alt: ghs-info.at("GHS0"+ str(index)).name + " sign", fit: fit)
}
155 changes: 155 additions & 0 deletions packages/preview/typsium-ghs/0.1.1/src/hp.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#let get-language-index(lang)={
if lang == "en"{1}
else if lang == "de"{0}
else if lang == "fr"{2}
else if lang == "it"{3}
else{1}
}

#let database = state("hp-statements", csv("resources/hp-statements.tsv", delimiter: "\t", row-type: dictionary))

#let validate-string(statement, substitute)={
if statement.contains(regex("p|P")){
substitute = "P"
} else if statement.contains(regex("h|H")){
substitute = "H"
}

statement = statement.replace(regex("[A-Za-z ,-]"), "")
statement = upper(statement)

statement = statement.replace(regex("(\d+)"), x=> substitute + x.text)

return statement
}

#let validate-statement(statement, substitute)={
if type(statement) == int {
substitute + str(statement)
} else if type(statement) == str{
validate-string(statement, substitute)
} else if type(statement) == content{
validate-string(statement.text(), substitute)
}
}

#let get-statement(statement, variant, parameters, only-statement, as-hover,default: x=> "no statement found: " + repr(x))={
let x = parameters.len()
let parameterlen = if type(parameters) == str{
"-1"
}else {
if parameters.len() > 0 {"-"+str(parameters.len())}else{""}
}

context {
let lang-index = get-language-index(text.lang)
let db = database.get().at(lang-index)
let full-statement
if variant != auto{
let variant = "."+ str(variant)
full-statement = db.at(statement + variant + parameterlen, default: db.at(statement + variant, default: db.at(statement, default: none)))
}else{
full-statement = db.at(statement + parameterlen, default: db.at(statement, default: none))
}

if full-statement == none {
if statement.contains("+"){
full-statement = statement.split("+").map(x=> db.at(x, default:"")+ " ").sum()
}
}

if full-statement == none {
full-statement = default(statement)
}

if type(full-statement) == str{
if type(parameters) == str{
full-statement = full-statement.replace("{1}", parameters)
} else if type(parameters) == array{
full-statement = full-statement.replace("{1}", parameters.at(0, default: "…"))
.replace("{2}", parameters.at(1, default: "…"))
}
}

if not only-statement{
full-statement = statement +": "+ full-statement
}

if as-hover{
return link(full-statement.replace(" ", "_"), statement)
}
return full-statement
}
}

///
/// Displays a hazard statement.
/// - statement (): the code of the statement to display
/// - variant (): selects which variant to display. only applicable for some statements
/// - parameters (): additional parameters can manually be added and are filled into the hp-statements based on the order they appear in
/// - only-statement (bool): should the code of the statement be displayed
/// - as-hover (bool): Will display statements only as their code, but provides the full statement inside a link so it is shown when hovering over it. May not work in all PDF viewers
/// - validate (): only change this if you are a plugin developer and know you can skip validation
/// -> content
#let h-statement(statement, variant: auto, parameters: (), only-statement:false, as-hover:false, validate: true)={
if validate{
statement = validate-statement(statement, "H")
}

return get-statement(statement, variant, parameters, only-statement, as-hover)
}

///
/// Displays a precautionary statement.
/// - statement (): the code of the statement to display
/// - variant (): selects which variant to display. only applicable for some statements
/// - parameters (): additional parameters can manually be added and are filled into the hp-statements based on the order they appear in
/// - only-statement (bool): should the code of the statement be displayed
/// - as-hover (bool): Will display statements only as their code, but provides the full statement inside a link so it is shown when hovering over it. May not work in all PDF viewers
/// - validate (): only change this if you are a plugin developer and know you can skip validation
/// -> content
#let p-statement(statement, variant: auto, parameters: (), only-statement:false,as-hover:false, validate: true)={
if validate{
statement = validate-statement(statement, "P")
}

return get-statement(statement, variant, parameters, only-statement, as-hover)
}
///
/// Displays a hazard or precautionary statement. The type is inferred from if the code contains an H or a P. if integers are provided it defaults to precautionary statements
/// - statement (): the code of the statement to display
/// - variant (): selects which variant to display. only applicable for some statements
/// - parameters (): additional parameters can manually be added and are filled into the hp-statements based on the order they appear in
/// - only-statement (bool): should the code of the statement be displayed
/// - as-hover (bool): Will display statements only as their code, but provides the full statement inside a link so it is shown when hovering over it. May not work in all PDF viewers
/// - validate (): only change this if you are a plugin developer and know you can skip validation
/// -> content
#let hp(statement, variant: auto, parameters: (), only-statement:false,as-hover:false, validate: true)= p-statement(statement, variant:variant, parameters:parameters, only-statement:only-statement, as-hover: as-hover, validate:validate)

#let split-statements(statements, validate: true)={
statements = if type(statements) == str{
statements.split(regex("[,\s\-]+")).filter(x=> x.len() != 0)
} else if type(statements) == array{
statements.map(x=> x.split(regex("[,\s\-]+"))).flatten().filter(x=> x.len() != 0)
}
if validate{
statements.map(x=> validate-statement(x, "P"))
}else{
statements
}
}

///
/// Displays multiple hazard and precautionary statements
/// - statements (str|array): a list of statement codes to display.
/// - only-statement (bool): should the code of the statement be displayed
/// - as-hover (bool): Will display statements only as their code, but provides the full statement inside a link so it is shown when hovering over it. May not work in all PDF viewers
/// - validate (bool): only change this if you are a plugin developer and know you can skip validation
/// -> content
#let display-statements(statements, only-statement:false, as-hover:false, validate: true)={
statements = split-statements(statements, validate:validate)
for value in statements {
hp(value, only-statement:only-statement,as-hover:as-hover, validate:validate)
linebreak()
}
}
2 changes: 2 additions & 0 deletions packages/preview/typsium-ghs/0.1.1/src/lib.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#import "ghs.typ": ghs, ghs-info
#import "hp.typ": h-statement, p-statement, hp, split-statements, display-statements
Loading