Merge pull request #196 from xiaoyifang/gd-master

sync upstream
This commit is contained in:
xiaoyifang 2022-11-06 12:03:19 +08:00 committed by GitHub
commit 5230d254a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 8 deletions

View file

@ -184,8 +184,8 @@ InputPhrase Preferences::sanitizeInputPhrase( QString const & inputPhrase ) cons
if( limitInputPhraseLength && inputPhrase.size() > inputPhraseLengthLimit )
{
gdWarning( "Ignoring an input phrase %d symbols long. The configured maximum input phrase length is %d symbols.",
inputPhrase.size(), inputPhraseLengthLimit );
gdDebug( "Ignoring an input phrase %d symbols long. The configured maximum input phrase length is %d symbols.",
inputPhrase.size(), inputPhraseLengthLimit );
return result;
}

View file

@ -12,6 +12,7 @@
#include <stdio.h>
#include <wctype.h>
#include <algorithm>
namespace Dsl {
@ -164,12 +165,37 @@ wstring ArticleDom::Node::renderAsText( bool stripTrsTag ) const
return result;
}
// Returns true if src == 'm' and dest is 'mX', where X is a digit
static inline bool checkM( wstring const & dest, wstring const & src )
namespace {
/// @return true if @p tagName equals "mN" where N is a digit
bool is_mN( wstring const & tagName )
{
return ( src == U"m" && dest.size() == 2 && dest[ 0 ] == L'm' && iswdigit( dest[ 1 ] ) );
return tagName.size() == 2 && tagName[ 0 ] == U'm' && iswdigit( tagName[ 1 ] );
}
bool isAnyM( wstring const & tagName )
{
return tagName == U"m" || is_mN( tagName );
}
bool checkM( wstring const & dest, wstring const & src )
{
return src == U"m" && is_mN( dest );
}
/// Closing the [mN] tags is optional. Quote from https://documentation.help/ABBYY-Lingvo8/paragraph_form.htm:
/// Any paragraph from this tag until the end of card or until system meets an «[/m]» (margin shift toggle off) tag
struct MustTagBeClosed
{
bool operator()( ArticleDom::Node const * tag ) const
{
Q_ASSERT( tag->isTag );
return !isAnyM( tag->tagName );
}
};
} // unnamed namespace
ArticleDom::ArticleDom( wstring const & str, string const & dictName,
wstring const & headword_):
root( Node::Tag(), wstring(), wstring() ), stringPos( str.c_str() ),
@ -375,7 +401,7 @@ ArticleDom::ArticleDom( wstring const & str, string const & dictName,
if ( !isClosing )
{
if( name == U"m" || ( name.size() == 2 && name[ 0 ] == L'm' && iswdigit( name[ 1 ] ) ) )
if( isAnyM( name ) )
{
// Opening an 'mX' or 'm' tag closes any previous 'm' tag
closeTag( U"m" , stack, false );
@ -630,7 +656,24 @@ ArticleDom::ArticleDom( wstring const & str, string const & dictName,
if ( stack.size() )
{
GD_FDPRINTF( stderr, "Warning: %u tags were unclosed.\n", (unsigned) stack.size() );
list< Node * >::iterator it = std::find_if( stack.begin(), stack.end(), MustTagBeClosed() );
if( it == stack.end() )
return; // no unclosed tags that must be closed => nothing to warn about
QByteArray const firstTagName = gd::toQString( ( *it )->tagName ).toUtf8();
++it;
unsigned const unclosedTagCount = 1 + std::count_if( it, stack.end(), MustTagBeClosed() );
if( dictName.empty() )
{
gdWarning( "Warning: %u tag(s) were unclosed, first tag name \"%s\".\n",
unclosedTagCount, firstTagName.constData() );
}
else
{
gdWarning( "Warning: %u tag(s) were unclosed in \"%s\", article \"%s\", first tag name \"%s\".\n",
unclosedTagCount, dictName.c_str(), gd::toQString( headword ).toUtf8().constData(),
firstTagName.constData() );
}
}
}
@ -640,7 +683,7 @@ void ArticleDom::openTag( wstring const & name,
{
list< Node > nodesToReopen;
if( name == U"m" || checkM( name, U"m" ) )
if( isAnyM( name ) )
{
// All tags above [m] tag will be closed and reopened after
// to avoid break this tag by closing some other tag.