# Copyright 1999-2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

RUST_MIN_VER="1.88.0"

CRATES="
	adler2@2.0.1
	aho-corasick@1.1.4
	base64@0.22.1
	bincode@1.3.3
	bindgen@0.72.1
	bit-set@0.8.0
	bit-vec@0.8.0
	bitflags@2.13.0
	caseless@0.2.2
	cc@1.2.65
	cexpr@0.6.0
	cfg-if@1.0.4
	clang-sys@1.8.1
	comrak@0.53.0
	crc32fast@1.5.0
	deranged@0.5.8
	either@1.16.0
	emojis@0.8.2
	entities@1.0.1
	equivalent@1.0.2
	fancy-regex@0.16.2
	fastrand@2.4.1
	find-msvc-tools@0.1.9
	finl_unicode@1.4.0
	flate2@1.1.9
	fnv@1.0.7
	glob@0.3.3
	hashbrown@0.17.1
	indexmap@2.14.0
	itertools@0.13.0
	itoa@1.0.18
	jetscii@0.5.3
	lazy_static@1.5.0
	libc@0.2.186
	libloading@0.8.9
	linked-hash-map@0.5.6
	magnus-macros@0.8.0
	magnus@0.8.2
	memchr@2.8.2
	minimal-lexical@0.2.1
	miniz_oxide@0.8.9
	nom@7.1.3
	num-conv@0.2.2
	once_cell@1.21.4
	onig@6.5.3
	onig_sys@69.9.3
	phf@0.13.1
	phf_codegen@0.13.1
	phf_generator@0.13.1
	phf_shared@0.13.1
	pkg-config@0.3.33
	plist@1.9.0
	powerfmt@0.2.0
	proc-macro2@1.0.106
	quick-xml@0.39.4
	quote@1.0.46
	rb-allocator@0.9.6
	rb-sys-build@0.9.128
	rb-sys-env@0.2.3
	rb-sys@0.9.128
	rctree@0.6.0
	regex-automata@0.4.14
	regex-syntax@0.8.11
	regex@1.12.4
	rustc-hash@2.1.2
	same-file@1.0.6
	seq-macro@0.3.6
	serde@1.0.228
	serde_core@1.0.228
	serde_derive@1.0.228
	serde_json@1.0.150
	shell-words@1.1.1
	shlex@1.3.0
	shlex@2.0.1
	simd-adler32@0.3.9
	siphasher@1.0.3
	smallvec@1.15.2
	syn@2.0.118
	syntect@5.3.0
	thiserror-impl@2.0.18
	thiserror@2.0.18
	time-core@0.1.9
	time-macros@0.2.31
	time@0.3.53
	tinyvec@1.11.0
	tinyvec_macros@0.1.1
	typed-arena@2.0.2
	unicode-ident@1.0.24
	unicode-normalization@0.1.25
	walkdir@2.5.0
	winapi-util@0.1.11
	windows-link@0.2.1
	windows-sys@0.61.2
	yaml-rust@0.4.5
	zmij@1.0.21
"

USE_RUBY="ruby32 ruby33 ruby34"

RUBY_FAKEGEM_EXTENSIONS=(ext/commonmarker/extconf.rb)
RUBY_FAKEGEM_EXTENSION_LIBDIR="lib/commonmarker"
RUBY_FAKEGEM_EXTRADOC="CHANGELOG.md README.md"

RUBY_FAKEGEM_GEMSPEC="commonmarker.gemspec"

inherit cargo ruby-fakegem

DESCRIPTION="A fast, safe, extensible parser for CommonMark, wrapping the comrak Rust crate"
HOMEPAGE="https://github.com/gjtorikian/commonmarker"
SRC_URI="https://github.com/gjtorikian/commonmarker/archive/v${PV}.tar.gz -> ${P}.tar.gz"
SRC_URI+=" ${CARGO_CRATE_URIS}"

LICENSE="MIT"
# Dependent crate licenses
LICENSE+=" Apache-2.0 BSD BSD-2 ISC MIT Unicode-3.0 Unicode-DFS-2016"
SLOT="0"
KEYWORDS="~amd64"

# bindgen (rb-sys) loads libclang on the build host
BDEPEND="llvm-core/clang"

ruby_add_bdepend ">=dev-ruby/rb_sys-0.9:0"

all_ruby_prepare() {
	cargo_src_unpack

	# Oniguruma stays bundled in onig_sys: with the system copy
	# (RUSTONIG_SYSTEM_LIBONIG=1) the tests crash, bug 951737.

	# rb_sys only builds the extension; as a runtime dependency of the
	# gemspec Bundler would refuse to load commonmarker without it.
	sed -i -e '/add_dependency("rb_sys"/d' ${RUBY_FAKEGEM_GEMSPEC} || die

	# Leave stripping and splitdebug to Portage. Done in the sources, not
	# with CARGO_PROFILE_RELEASE_STRIP: "make install" runs cargo again and
	# a profile that differs from the compile phase rebuilds the library.
	sed -i -e '/^strip = true$/d' Cargo.toml || die

	sed -i -e '/focus/ s:^:#:' test/test_helper.rb || die
}

each_ruby_test() {
	${RUBY} -Ilib:test:. -e 'Dir["test/*_test.rb"].each {|f| require f}' || die
}