2012-02-20 21:47:14 +00:00
|
|
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
2009-01-28 20:55:45 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#ifndef __ARTICLE_MAKER_HH_INCLUDED__
|
|
|
|
#define __ARTICLE_MAKER_HH_INCLUDED__
|
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
#include <QObject>
|
2009-05-29 19:48:50 +00:00
|
|
|
#include <QMap>
|
2009-03-26 19:00:08 +00:00
|
|
|
#include <set>
|
|
|
|
#include <list>
|
2021-06-10 16:13:11 +00:00
|
|
|
#include "config.hh"
|
2009-01-28 20:55:45 +00:00
|
|
|
#include "dictionary.hh"
|
|
|
|
#include "instances.hh"
|
2009-04-17 13:51:50 +00:00
|
|
|
#include "wordfinder.hh"
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
/// This class generates the article's body for the given lookup request
|
2009-02-01 00:08:08 +00:00
|
|
|
class ArticleMaker: public QObject
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-02-01 00:08:08 +00:00
|
|
|
Q_OBJECT // We make it QObject to use tr() conveniently
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
std::vector< sptr< Dictionary::Class > > const & dictionaries;
|
|
|
|
std::vector< Instances::Group > const & groups;
|
2023-03-21 06:00:23 +00:00
|
|
|
const Config::Preferences & cfg;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// On construction, a reference to all dictionaries and a reference all
|
|
|
|
/// groups' instances are to be passed. Those references are kept stored as
|
|
|
|
/// references, and as such, any changes to them would reflect on the results
|
2010-03-30 13:41:14 +00:00
|
|
|
/// of the inquiries, although those changes are perfectly legal.
|
2023-03-21 07:58:58 +00:00
|
|
|
ArticleMaker( std::vector< sptr< Dictionary::Class > > const & dictionaries,
|
|
|
|
std::vector< Instances::Group > const & groups,
|
|
|
|
const Config::Preferences & cfg );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2021-06-10 16:13:11 +00:00
|
|
|
/// Looks up the given phrase within the given group, and creates a full html
|
2009-01-28 20:55:45 +00:00
|
|
|
/// page text containing its definition.
|
2009-03-26 19:00:08 +00:00
|
|
|
/// The result is returned as Dictionary::DataRequest just like dictionaries
|
|
|
|
/// themselves do. The difference is that the result is a complete html page
|
|
|
|
/// with all definitions from all the relevant dictionaries.
|
2009-05-29 19:48:50 +00:00
|
|
|
/// Contexts is a map of context values to be passed to each dictionary, where
|
|
|
|
/// the keys are dictionary ids.
|
2009-09-23 18:44:38 +00:00
|
|
|
/// If mutedDicts is not empty, the search would be limited only to those
|
|
|
|
/// dictionaries in group which aren't listed there.
|
2021-06-10 16:13:11 +00:00
|
|
|
sptr< Dictionary::DataRequest > makeDefinitionFor( Config::InputPhrase const & phrase, unsigned groupId,
|
2009-09-23 18:44:38 +00:00
|
|
|
QMap< QString, QString > const & contexts,
|
|
|
|
QSet< QString > const & mutedDicts =
|
2014-04-16 16:18:28 +00:00
|
|
|
QSet< QString >(),
|
2018-06-13 16:00:42 +00:00
|
|
|
QStringList const & dictIDs = QStringList(),
|
|
|
|
bool ignoreDiacritics = false ) const;
|
2009-02-01 00:08:08 +00:00
|
|
|
|
|
|
|
/// Makes up a text which states that no translation for the given word
|
|
|
|
/// was found. Sometimes it's better to call this directly when it's already
|
|
|
|
/// known that there's no translation.
|
2009-03-26 19:00:08 +00:00
|
|
|
sptr< Dictionary::DataRequest > makeNotFoundTextFor( QString const & word, QString const & group ) const;
|
2009-02-01 00:08:08 +00:00
|
|
|
|
2009-05-11 15:33:57 +00:00
|
|
|
/// Creates an 'untitled' page. The result is guaranteed to be instant.
|
|
|
|
sptr< Dictionary::DataRequest > makeEmptyPage() const;
|
|
|
|
|
2012-12-07 11:59:29 +00:00
|
|
|
/// Create page with one picture
|
|
|
|
sptr< Dictionary::DataRequest > makePicturePage( std::string const & url ) const;
|
|
|
|
|
2012-12-13 20:21:33 +00:00
|
|
|
/// Add base path to file path if it's relative and file not found
|
|
|
|
/// Return true if path successfully adjusted
|
|
|
|
static bool adjustFilePath( QString & fileName );
|
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
private:
|
2022-01-12 13:48:03 +00:00
|
|
|
std::string readCssFile(QString const& fileName, std::string type) const;
|
2009-02-01 00:08:08 +00:00
|
|
|
/// Makes everything up to and including the opening body tag.
|
2014-04-22 13:47:02 +00:00
|
|
|
std::string makeHtmlHeader( QString const & word, QString const & icon,
|
|
|
|
bool expandOptionalParts ) const;
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
/// Makes the html body for makeNotFoundTextFor()
|
|
|
|
static std::string makeNotFoundBody( QString const & word, QString const & group );
|
|
|
|
|
|
|
|
friend class ArticleRequest; // Allow it calling makeNotFoundBody()
|
2009-01-28 20:55:45 +00:00
|
|
|
};
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
/// The request specific to article maker. This should really be private,
|
|
|
|
/// but we need it to be handled by moc.
|
|
|
|
class ArticleRequest: public Dictionary::DataRequest
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
QString word, group;
|
2009-05-29 19:48:50 +00:00
|
|
|
QMap< QString, QString > contexts;
|
2009-09-23 18:44:38 +00:00
|
|
|
std::vector< sptr< Dictionary::Class > > activeDicts;
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2009-04-18 17:20:12 +00:00
|
|
|
std::set< gd::wstring > alts; // Accumulated main forms
|
2009-03-26 19:00:08 +00:00
|
|
|
std::list< sptr< Dictionary::WordSearchRequest > > altSearches;
|
|
|
|
bool altsDone, bodyDone;
|
|
|
|
std::list< sptr< Dictionary::DataRequest > > bodyRequests;
|
|
|
|
bool foundAnyDefinitions;
|
2009-04-12 16:22:42 +00:00
|
|
|
bool closePrevSpan; // Indicates whether the last opened article span is to
|
|
|
|
// be closed after the article ends.
|
2009-04-17 13:51:50 +00:00
|
|
|
sptr< WordFinder > stemmedWordFinder; // Used when there're no results
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2010-03-30 13:41:14 +00:00
|
|
|
/// A sequence of words and spacings between them, including the initial
|
|
|
|
/// spacing before the first word and the final spacing after the last word.
|
|
|
|
typedef QList< QString > Words;
|
|
|
|
typedef QList< QString > Spacings;
|
|
|
|
|
|
|
|
/// Splits the given string into words and spacings between them.
|
|
|
|
QPair< Words, Spacings > splitIntoWords( QString const & );
|
|
|
|
|
|
|
|
QPair< Words, Spacings > splittedWords;
|
|
|
|
int currentSplittedWordStart;
|
|
|
|
int currentSplittedWordEnd;
|
|
|
|
QString currentSplittedWordCompound;
|
2010-05-29 20:50:16 +00:00
|
|
|
QString lastGoodCompoundResult;
|
2010-03-30 13:41:14 +00:00
|
|
|
bool firstCompoundWasFound;
|
2013-06-02 11:20:33 +00:00
|
|
|
int articleSizeLimit;
|
2013-06-03 13:38:57 +00:00
|
|
|
bool needExpandOptionalParts;
|
2018-06-13 16:00:42 +00:00
|
|
|
bool ignoreDiacritics;
|
2010-03-30 13:41:14 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
public:
|
|
|
|
|
2021-06-10 16:13:11 +00:00
|
|
|
ArticleRequest( Config::InputPhrase const & phrase, QString const & group,
|
2009-05-29 19:48:50 +00:00
|
|
|
QMap< QString, QString > const & contexts,
|
2009-03-26 19:00:08 +00:00
|
|
|
std::vector< sptr< Dictionary::Class > > const & activeDicts,
|
2013-06-02 11:20:33 +00:00
|
|
|
std::string const & header,
|
2018-06-13 16:00:42 +00:00
|
|
|
int sizeLimit, bool needExpandOptionalParts_,
|
|
|
|
bool ignoreDiacritics = false );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2011-12-12 16:52:07 +00:00
|
|
|
virtual void cancel();
|
|
|
|
// { finish(); } // Add our own requests cancellation here
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
private slots:
|
|
|
|
|
|
|
|
void altSearchFinished();
|
|
|
|
void bodyFinished();
|
2009-04-17 13:51:50 +00:00
|
|
|
void stemmedSearchFinished();
|
2010-03-30 13:41:14 +00:00
|
|
|
void individualWordFinished();
|
|
|
|
|
|
|
|
private:
|
2022-04-20 12:53:57 +00:00
|
|
|
int htmlTextSize( QString html );
|
2010-03-30 13:41:14 +00:00
|
|
|
/// Appends the given string to 'data', with locking its mutex.
|
|
|
|
void appendToData( std::string const & );
|
|
|
|
|
|
|
|
/// Uses stemmedWordFinder to perform the next step of looking up word
|
|
|
|
/// combinations.
|
|
|
|
void compoundSearchNextStep( bool lastSearchSucceeded );
|
|
|
|
|
|
|
|
/// Creates a single word out of the [currentSplittedWordStart..End] range.
|
|
|
|
QString makeSplittedWordCompound();
|
|
|
|
|
|
|
|
/// Makes an html link to the given word.
|
|
|
|
std::string linkWord( QString const & );
|
|
|
|
|
|
|
|
/// Escapes the spacing between the words to include in html.
|
|
|
|
std::string escapeSpacing( QString const & );
|
2013-06-03 13:38:57 +00:00
|
|
|
|
|
|
|
/// Find end of corresponding </div> tag
|
|
|
|
int findEndOfCloseDiv( QString const &, int pos );
|
2009-03-26 19:00:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
#endif
|