# HG changeset patch
# Node ID 8f3bc5c16e6e
# Parent  d5968834be1c
diff --git a/.hgignore b/.hgignore
--- a/.hgignore
+++ b/.hgignore
@@ -1,3 +1,4 @@
 syntax: glob
 build**/*
+subprojects/*/*
 
diff --git a/meson.build b/meson.build
--- a/meson.build
+++ b/meson.build
@@ -1,13 +1,41 @@
 project('libtraversity', 'C', version : '0.0.1')
 
+###############################################################################
+# Module imports
+###############################################################################
 gnome = import('gnome')
 
+###############################################################################
+# Basic Variables
+###############################################################################
+toplevel_inc = include_directories('.')
+
+###############################################################################
+# Main Dependencies
+###############################################################################
 GLIB = dependency('glib-2.0', version : '>=2.70.0')
 GOBJECT = dependency('gobject-2.0', version : '>=2.70.0')
 
 TEMPLATE_GLIB = dependency('template-glib-1.0', version : '>=3.35.0')
 
-toplevel_inc = include_directories('.')
+###############################################################################
+# Documentation
+###############################################################################
+if get_option('doc') and not get_option('introspection')
+    error('Documentation requires GObject Introspection.')
+endif
 
+gidocgen_dep = dependency(
+    'gi-docgen', version: '>= 2021.1',
+    fallback: ['gi-docgen', 'dummy_dep'],
+    required: get_option('doc')
+)
+
+gidocgen = find_program('gi-docgen', required : get_option('doc'))
+docs_dir = get_option('prefix') / get_option('datadir') / 'doc'
+
+###############################################################################
+# Subdirectories
+###############################################################################
 subdir('traversity')
 subdir('traversity-test')
diff --git a/meson_options.txt b/meson_options.txt
new file mode 100644
--- /dev/null
+++ b/meson_options.txt
@@ -0,0 +1,5 @@
+option('doc', type : 'boolean', value : false,
+       description : 'build documentation')
+
+option('introspection', type : 'boolean', value : true,
+       description : 'build introspection data')
diff --git a/subprojects/gi-docgen.wrap b/subprojects/gi-docgen.wrap
new file mode 100644
--- /dev/null
+++ b/subprojects/gi-docgen.wrap
@@ -0,0 +1,5 @@
+[wrap-file]
+directory = gi-docgen-2022.1
+source_url = https://download.gnome.org/sources/gi-docgen/2022/gi-docgen-2022.1.tar.xz
+source_filename = gi-docgen-2022.1.tar.xz
+source_hash = fefe3153979595ddad2dc81d5efa7e54c8ca3770f91a5a75710c8fd6fad4575f
diff --git a/traversity/reference/meson.build b/traversity/reference/meson.build
new file mode 100644
--- /dev/null
+++ b/traversity/reference/meson.build
@@ -0,0 +1,31 @@
+traversity_doc_content_files = []
+
+if get_option('doc')
+	traversity_toml = configure_file(
+		input : 'traversity.toml.in',
+		output : 'traversity.toml',
+		configuration : version_conf,
+		install : true,
+		install_dir : docs_dir / 'traversity',
+	)
+
+	traversity_doc = custom_target('traversity-doc',
+		input : [ traversity_toml, traversity_gir[0] ],
+		output : 'traversity',
+		command : [
+			gidocgen,
+			'generate',
+			'--quiet',
+			'--fatal-warnings',
+			'--config=@INPUT0@',
+			'--output-dir=@OUTPUT@',
+			'--no-namespace-dir',
+			'--content-dir=@0@'.format(meson.current_source_dir()),
+			'@INPUT1@'
+		],
+		depend_files : [ traversity_doc_content_files ],
+		build_by_default : true,
+		install : true,
+		install_dir : docs_dir,
+	)
+endif
diff --git a/traversity/reference/traversity.toml.in b/traversity/reference/traversity.toml.in
new file mode 100644
--- /dev/null
+++ b/traversity/reference/traversity.toml.in
@@ -0,0 +1,38 @@
+[library]
+version = "@TRAVERSITY_VERSION@"
+browse_url = "https://keep.imfreedom.org/traversity/traversity/"
+repository_url = "https://keep.imfreedom.org/traversity/traversity/"
+website_url = "https://keep.imfreedom.org/traversity/traversity/"
+authors = "Traversity Developers"
+logo_url = ""
+license = "LGPL-2.1-or-later"
+description = "Traversity NAT Traversal Library"
+dependencies = [ "GLib-2.0", "GObject-2.0" ]
+devhelp = true
+search_index = true
+
+  [dependencies."GLib-2.0"]
+  name = "GLib"
+  description = "General-purpose, portable utility library."
+  docs_url = "https://docs.gtk.org/glib/"
+
+  [dependencies."GObject-2.0"]
+  name = "GObject"
+  description = "The base type system library"
+  docs_url = "https://docs.gtk.org/gobject/"
+
+[theme]
+name = "basic"
+show_index_summary = true
+show_class_hierarchy = true
+
+[source-location]
+base_url = "https://keep.imfreedom.org/traversity/traversity/file/default/"
+
+[extra]
+# The same order will be used when generating the index
+content_files = [
+]
+content_images = [
+]
+urlmap_file = "urlmap.js"
diff --git a/traversity/reference/urlmap.js b/traversity/reference/urlmap.js
new file mode 100644
--- /dev/null
+++ b/traversity/reference/urlmap.js
@@ -0,0 +1,8 @@
+// SPDX-FileCopyrightText: 2021 GNOME Foundation
+// SPDX-License-Identifier: LGPL-2.1-or-later
+
+// A map between namespaces and base URLs for their online documentation
+baseURLs = [
+    [ 'GLib', 'https://docs.gtk.org/glib/' ],
+    [ 'GObject', 'https://docs.gtk.org/gobject/' ],
+]
