]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - tools/mkdescriptions
Sync helpop chmodes s and p with docs
[user/henk/code/inspircd.git] / tools / mkdescriptions
1 #!/usr/bin/env perl
2 #
3 # InspIRCd -- Internet Relay Chat Daemon
4 #
5 #   Copyright (C) 2020-2021 Sadie Powell <sadie@witchery.services>
6 #
7 # This file is part of InspIRCd.  InspIRCd is free software: you can
8 # redistribute it and/or modify it under the terms of the GNU General Public
9 # License as published by the Free Software Foundation, version 2.
10 #
11 # This program is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14 # details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20
21 use v5.10.0;
22 use strict;
23 use warnings FATAL => qw(all);
24
25 use File::Basename          qw(basename dirname);
26 use File::Spec::Functions   qw(catdir catfile rel2abs);
27 use FindBin                 qw($RealDir);
28 use HTML::FormatText        ();
29 use HTML::TreeBuilder       ();
30 use Text::Markdown::Hoedown qw(HOEDOWN_HTML_SKIP_HTML markdown);
31 use Text::Sentence          qw(split_sentences);
32
33 use lib dirname $RealDir;
34 use make::common;
35 use make::console;
36
37 unless (scalar @ARGV) {
38         say STDERR console_format "<|GREEN Usage:|> $0 <<|UNDERLINE DOCS-SITE|>>";
39         exit 1;
40 }
41
42 my %version = get_version();
43 my $docdir = rel2abs catdir $ARGV[0], 'docs', $version{MAJOR}, 'modules';
44 print_error "unable to find the module directory at $docdir!" unless -d $docdir;
45
46 my $root = dirname $RealDir;
47 for my $module (<$root/src/modules/extra/m_*.cpp>, <$root/src/modules/m_*.cpp>, <$root/src/modules/m_*/main.cpp>) {
48         print_error "unable to extract module name from $module!" unless $module =~ /m_(\w+)[.\/]/;
49         my $docfile = catfile $docdir, "$1.md";
50         print_error "unable to find the module documentation at $docfile!" unless -f $docfile;
51
52         open(my $dh, $docfile) or print_error "unable to read from $docfile: $!";
53         my $docdata = do { local $/; <$dh> };
54         close $dh;
55         print_error "unable to find the module description in $docfile!" unless $docdata =~ /\#\#\# Description\n\n(?:This module )?([^\n]+)/;
56
57         my $docrendered = markdown ucfirst $1, extensions => HOEDOWN_HTML_SKIP_HTML;
58         my $docplain = HTML::FormatText->new(leftmargin => 0, rightmargin => ~0)->format(HTML::TreeBuilder->new->parse($docrendered));
59
60         my $description = (split_sentences $docplain)[0] =~ s/"/\\"/gr;
61         chomp($description);
62
63         open(my $mih, $module) or print_error "unable to read from $module: $!";
64         my @lines;
65         for my $line (<$mih>) {
66                 chomp $line;
67                 if ($line =~ /(\t+return Version\(")[^"]+(",.+)/) {
68                         push @lines, join '', $1, $description, $2;
69                 } else {
70                         push @lines, $line;
71                 }
72         }
73         close $mih;
74
75         open(my $moh, '>', $module) or print_error "unable to write to $module: $!";
76         for my $line (@lines) {
77                 say $moh $line;
78         }
79         close $moh;
80 }