goldendict-ng/src/metadata.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

53 lines
1.6 KiB
C++
Raw Normal View History

#include "metadata.hh"
#include "toml++/toml.hpp"
#include <QDebug>
#include <QFile>
std::optional< Metadata::result > Metadata::load( std::string_view filepath )
{
if ( !QFile::exists( QString::fromStdString( std::string{ filepath } ) ) ) {
return std::nullopt;
}
// by default, the optional will be initialized as std::nullopt
Metadata::result result{};
toml::table tbl;
try {
tbl = toml::parse_file( filepath );
}
catch ( toml::parse_error & e ) {
qWarning() << "Failed to load metadata: " << QString::fromUtf8( filepath.data(), filepath.size() )
<< "Reason:" << e.what();
return std::nullopt;
}
if ( toml::array * categories = tbl.get_as< toml::array >( "categories" ) ) {
// result.categories is an optional, it exists, but the vector does not, so we have to create one here
result.categories.emplace();
for ( auto & el : *categories ) {
if ( el.is_string() ) {
result.categories.value().emplace_back( std::move( *el.value_exact< std::string >() ) );
}
}
}
result.name = tbl[ "metadata" ][ "name" ].value_exact< std::string >();
feat: customize dictionary's fullindex option with metadata.toml (#1000) * feat: customize dictionary's fullindex option with metadata.toml * [autofix.ci] apply automated fixes * feat: display fulltext status in the dictionary info dialog * [autofix.ci] apply automated fixes * doc: add document about how to enable dictionary's fullindex feature * Update src/metadata.cc Co-authored-by: shenleban tongying <shenlebantongying@gmail.com> * Update src/ui/dictinfo.ui Co-authored-by: shenleban tongying <shenlebantongying@gmail.com> * Update website/mkdocs.yml Co-authored-by: shenleban tongying <shenlebantongying@gmail.com> * Update website/docs/custom_dictionary.md Co-authored-by: shenleban tongying <shenlebantongying@gmail.com> * Update website/docs/custom_dictionary.md Co-authored-by: shenleban tongying <shenlebantongying@gmail.com> * Update website/docs/custom_dictionary.md Co-authored-by: shenleban tongying <shenlebantongying@gmail.com> * Update website/docs/custom_dictionary.md Co-authored-by: shenleban tongying <shenlebantongying@gmail.com> * Update website/docs/custom_dictionary.md Co-authored-by: shenleban tongying <shenlebantongying@gmail.com> * Update website/docs/custom_dictionary.md Co-authored-by: shenleban tongying <shenlebantongying@gmail.com> * add images * feat: show fulltext search status in dictionary info dialog * Update dictinfo.cc * [autofix.ci] apply automated fixes * ui: change layout --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: YiFang Xiao <yifang.xiao@noreply.com> Co-authored-by: shenleban tongying <shenlebantongying@gmail.com>
2023-07-26 02:03:20 +00:00
const auto fullindex = tbl[ "fts" ];
if ( fullindex.as_string() ) {
const auto value = fullindex.as_string()->get();
result.fullindex = value == "1" || value == "on" || value == "true";
}
else if ( fullindex.as_boolean() ) {
auto value = fullindex.as_boolean()->get();
result.fullindex = value;
}
else if ( fullindex.as_integer() ) {
const auto value = fullindex.as_integer()->get();
result.fullindex = value > 0;
}
return result;
}