From 27b3e364e6267bb7a0e5744729b31c292f9f3030 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Mon, 1 Mar 2021 11:40:58 -0700 Subject: [PATCH] Girocco/Project.pm: add _normalize_rmtype function Add a few extra aliases for possible incoming values of the 'girocco.rmtype' config value. Notably various possible file name extensions as well as content-type names. Provide a semi-private _normalize_rmtype function that returns one of the three "normalized" rmtype values for its single argument or optionally "" if unrecognized type. Signed-off-by: Kyle J. McKay --- Girocco/Project.pm | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/Girocco/Project.pm b/Girocco/Project.pm index 2429fea..11897d4 100644 --- a/Girocco/Project.pm +++ b/Girocco/Project.pm @@ -309,27 +309,48 @@ sub _cleanup_readme { my %rmtypes; BEGIN { %rmtypes = ( default => 'Markdown', + + 'text/markdown' => 'Markdown', + 'text/x-markdown' => 'Markdown', markdown => 'Markdown', + md => 'Markdown', + + 'text/plain' => 'Plain Text', 'plain text' => 'Plain Text', + 'plain-text' => 'Plain Text', + plain_text => 'Plain Text', + plaintext => 'Plain Text', plain => 'Plain Text', text => 'Plain Text', - html => 'HTML' + txt => 'Plain Text', + + 'text/html' => 'HTML', + html => 'HTML', + htm => 'HTML' ) } +# if $_[1] is true, return "" instead of default for unknown type +sub _normalize_rmtype { + my $rmtype = shift; + my $nodefault = shift; + defined($rmtype) and $rmtype = $rmtypes{lc($rmtype)}; + defined($rmtype) && $rmtype ne '' or + $rmtype = $nodefault ? "" : $rmtypes{'default'}; + return $rmtype; +} + sub _lint_readme { my $self = shift; my $htmlfrag = shift; defined($htmlfrag) or $htmlfrag = 1; defined($self->{READMEDATA}) or $self->{READMEDATA} = ''; - my $rmtype = $rmtypes{lc($self->{rmtype} || '')}; - defined($rmtype) && $rmtype ne '' or $rmtype = $rmtypes{'default'}; - $self->{rmtype} = $rmtype; + $self->{rmtype} = _normalize_rmtype($self->{rmtype}); # Empty always just becomes empty with no errors if ($self->{READMEDATA} eq '') { $self->{README} = ''; return 0; } - if ($rmtype eq 'Plain Text') { + if ($self->{rmtype} eq 'Plain Text') { # Never any errors with this one my $esc = $self->{READMEDATA}; $esc =~ s/&/&/gs; @@ -341,7 +362,7 @@ sub _lint_readme { return (1, "README: Markdown/HTML formats temporarily unavailable"); }; my @errs = (); - if ($rmtype eq 'HTML') { + if ($self->{rmtype} eq 'HTML') { # Legacy HTML support but now sanitized with possible auto-fixup my $output; eval { $output = Markdown::ProcessRaw($self->{READMEDATA}, @@ -433,8 +454,7 @@ sub _properties_load { $self->_cleanup_datetime('lastactivity'); $self->_cleanup_description; $self->_cleanup_readme; - defined($self->{rmtype}) and $self->{rmtype} = $rmtypes{lc($self->{rmtype})}; - defined($self->{rmtype}) or $self->{rmtype} = ""; + $self->{rmtype} = _normalize_rmtype($self->{rmtype}, 1); if (!$self->{rmtype}) { # default type is "Markdown" unless README ne '' but READMEDATA eq '' if ($self->{README} ne '' && $self->{READMEDATA} eq '') { @@ -442,7 +462,7 @@ sub _properties_load { $self->{rmtype} = 'HTML'; $self->{READMEDATA} = $self->{README}; } else { - $self->{rmtype} = $rmtypes{'default'}; + $self->{rmtype} = _normalize_rmtype(""); # get default type } } delete $self->{configfilehash}; -- 2.11.4.GIT