DSL: Trim spaces in "s" and "url" tags

This commit is contained in:
Abs62 2019-09-26 19:40:39 +03:00
parent 58560e1ef5
commit 02930198a8
3 changed files with 14 additions and 13 deletions

4
dsl.cc
View file

@ -858,7 +858,7 @@ string DslDictionary::nodeToHtml( ArticleDom::Node const & node )
else
if ( node.tagName == GD_NATIVE_TO_WS( L"s" ) || node.tagName == GD_NATIVE_TO_WS( L"video" ) )
{
string filename = Utf8::encode( node.renderAsText() );
string filename = Filetype::simplifyString( Utf8::encode( node.renderAsText() ), false );
string n =
getDictionaryFilenames()[ 0 ] + ".files" +
FsEncoding::separator() +
@ -999,7 +999,7 @@ string DslDictionary::nodeToHtml( ArticleDom::Node const & node )
else
if ( node.tagName == GD_NATIVE_TO_WS( L"url" ) )
{
string link = Html::escape( Utf8::encode( node.renderAsText() ) );
string link = Html::escape( Filetype::simplifyString( Utf8::encode( node.renderAsText() ), false ) );
if( QUrl::fromEncoded( link.c_str() ).scheme().isEmpty() )
link = "http://" + link;

View file

@ -9,11 +9,19 @@ namespace Filetype {
namespace {
/// Checks if the given string ends with the given substring
bool endsWith( string const & str, string const & tail )
{
return str.size() >= tail.size() &&
str.compare( str.size() - tail.size(), tail.size(), tail ) == 0;
}
}
/// Removes any trailing or leading spaces and lowercases the string.
/// The lowercasing is done simplistically, but it is enough for file
/// extensions.
string simplifyString( string const & str )
string simplifyString( string const & str, bool lowercase )
{
string result;
@ -33,20 +41,11 @@ string simplifyString( string const & str )
result.reserve( endPos - beginPos );
while( beginPos < endPos )
result.push_back( tolower( str[ beginPos++ ] ) );
result.push_back( lowercase ? tolower( str[ beginPos++ ] ) : str[ beginPos++ ] );
return result;
}
/// Checks if the given string ends with the given substring
bool endsWith( string const & str, string const & tail )
{
return str.size() >= tail.size() &&
str.compare( str.size() - tail.size(), tail.size(), tail ) == 0;
}
}
bool isNameOfSound( string const & name )
{
string s = simplifyString( name );

View file

@ -11,6 +11,8 @@ namespace Filetype {
using std::string;
/// Removes any trailing or leading spaces and may lowercases the string.
string simplifyString( string const & str, bool lowercase = true );
/// Returns true if the name resembles the one of a sound file (i.e. ends
/// with .wav, .ogg and such).
bool isNameOfSound( string const & );