Module: Typst

Defined in:
lib/base.rb,
lib/query.rb,
lib/typst.rb,
lib/document.rb,
lib/formats/pdf.rb,
lib/formats/png.rb,
lib/formats/svg.rb,
lib/formats/html_experimental.rb

Defined Under Namespace

Classes: Base, Document, HtmlExperimental, HtmlExperimentalDocument, Pdf, PdfDocument, Png, PngDocument, Query, Svg, SvgDocument

Constant Summary collapse

@@formats =
{}

Class Method Summary collapse

Class Method Details

.build_world_from_s(main_source, **options, &blk) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/typst.rb', line 20

def self.build_world_from_s(main_source, **options, &blk)
  dependencies = options[:dependencies] ||= {}
  fonts = options[:fonts] ||= {}

  Dir.mktmpdir do |tmp_dir|
    tmp_main_file = Pathname.new(tmp_dir).join("main.typ")
    File.write(tmp_main_file, main_source)

    dependencies.each do |dep_name, dep_source|
      tmp_dep_file = Pathname.new(tmp_dir).join(dep_name)
      File.write(tmp_dep_file, dep_source)
    end

    relative_font_path = Pathname.new(tmp_dir).join("fonts")
    relative_font_path.mkpath
    fonts.each do |font_name, font_bytes|
      tmp_font_file = relative_font_path.join(font_name)
      File.write(tmp_font_file, font_bytes)
    end

    options[:file] = tmp_main_file
    options[:root] = tmp_dir
    options[:font_paths] = [relative_font_path]

    blk.call(options)
  end
end

.build_world_from_zip(zip_file_path, main_file = "main.typ", **options, &blk) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/typst.rb', line 48

def self.build_world_from_zip(zip_file_path, main_file = "main.typ", **options, &blk)
  options[:dependencies] ||= {}
  options[:fonts] ||= {}

  Zip::File.open(zip_file_path) do |zipfile|
    file_names = zipfile.dir.glob("*").collect{ |f| f.name }
    case
      when file_names.include?(main_file) then tmp_main_file = main_file
      when file_names.include?("main.typ") then tmp_main_file = "main.typ"
      when file_names.size == 1 then tmp_main_file = file_names.first
      else raise "no main file found"
    end
    main_source = zipfile.file.read(tmp_main_file)
    file_names.delete(tmp_main_file)
    file_names.delete("fonts/")

    file_names.each do |dep_name|
      options[:dependencies][dep_name] = zipfile.file.read(dep_name)
    end

    font_file_names = zipfile.dir.glob("fonts/*").collect{ |f| f.name }
    font_file_names.each do |font_name|
      options[:fonts][Pathname.new(font_name).basename.to_s] = zipfile.file.read(font_name)
    end

    options[:main_file] = tmp_main_file

    build_world_from_s(main_source, **options, &blk)
  end
end

.clear_cache(max_age = 0) ⇒ Object



16
17
18
# File 'lib/typst.rb', line 16

def self.clear_cache(max_age = 0)
  Typst::_clear_cache(max_age)
end

.formatsObject



12
13
14
# File 'lib/typst.rb', line 12

def self.formats
  @@formats
end

.register_format(**format) ⇒ Object



8
9
10
# File 'lib/typst.rb', line 8

def self.register_format(**format)
  @@formats.merge!(format)
end