summaryrefslogtreecommitdiff
path: root/tools/mkdescriptions
blob: bb6f011d8a11fb2d5fb4d901a378b44bf1ba1b51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
47
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
78
79
80
#!/usr/bin/env perl
#
# InspIRCd -- Internet Relay Chat Daemon
#
#   Copyright (C) 2020 Sadie Powell <sadie@witchery.services>
#
# This file is part of InspIRCd.  InspIRCd is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#


use v5.10.0;
use strict;
use warnings FATAL => qw(all);

use File::Basename          qw(basename dirname);
use File::Spec::Functions   qw(catdir catfile rel2abs);
use FindBin                 qw($RealDir);
use HTML::FormatText        ();
use HTML::TreeBuilder       ();
use Text::Markdown::Hoedown qw(HOEDOWN_HTML_SKIP_HTML markdown);
use Text::Sentence          qw(split_sentences);

use lib dirname $RealDir;
use make::common;
use make::console;

unless (scalar @ARGV) {
	print_format "<|GREEN Usage:|> $0 <<|UNDERLINE DOCS-SITE|>>\n", *STDERR;
	exit 1;
}

my %version = get_version();
my $docdir = rel2abs catdir $ARGV[0], 'docs', $version{MAJOR}, 'modules';
print_error "unable to find the module directory at $docdir!" unless -d $docdir;

my $root = dirname $RealDir;
for my $module (<$root/src/modules/extra/m_*.cpp>, <$root/src/modules/m_*.cpp>, <$root/src/modules/m_*/main.cpp>) {
	print_error "unable to extract module name from $module!" unless $module =~ /m_(\w+)[.\/]/;
	my $docfile = catfile $docdir, "$1.md";
	print_error "unable to find the module documentation at $docfile!" unless -f $docfile;

	open(my $dh, $docfile) or print_error "unable to read from $docfile: $!";
	my $docdata = do { local $/; <$dh> };
	close $dh;
	print_error "unable to find the module description in $docfile!" unless $docdata =~ /\#\#\# Description\n\n(?:This module )?([^\n]+)/;

	my $docrendered = markdown ucfirst $1, extensions => HOEDOWN_HTML_SKIP_HTML;
	my $docplain = HTML::FormatText->new(leftmargin => 0, rightmargin => ~0)->format(HTML::TreeBuilder->new->parse($docrendered));

	my $description = (split_sentences $docplain)[0] =~ s/"/\\"/gr;
	chomp($description);

	open(my $mih, $module) or print_error "unable to read from $module: $!";
	my @lines;
	for my $line (<$mih>) {
		chomp $line;
		if ($line =~ /(\t+return Version\(")[^"]+(",.+)/) {
			push @lines, join '', $1, $description, $2;
		} else {
			push @lines, $line;
		}
	}
	close $mih;

	open(my $moh, '>', $module) or print_error "unable to write to $module: $!";
	for my $line (@lines) {
		say $moh $line;
	}
	close $moh;
}