File indexing completed on 2025-01-05 05:22:00
0001 /* 0002 * Copyright (c) 2018 Sune Vuorela <sune@vuorela.dk> 0003 * 0004 * Permission is hereby granted, free of charge, to any person 0005 * obtaining a copy of this software and associated documentation 0006 * files (the "Software"), to deal in the Software without 0007 * restriction, including without limitation the rights to use, 0008 * copy, modify, merge, publish, distribute, sublicense, and/or sell 0009 * copies of the Software, and to permit persons to whom the 0010 * Software is furnished to do so, subject to the following 0011 * conditions: 0012 * 0013 * The above copyright notice and this permission notice shall be 0014 * included in all copies or substantial portions of the Software. 0015 * 0016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 0017 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 0018 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 0019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 0020 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 0021 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 0022 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 0023 * OTHER DEALINGS IN THE SOFTWARE. 0024 */ 0025 0026 #include "recipeparser.h" 0027 #include <QIODevice> 0028 #include <QDebug> 0029 0030 enum class State { 0031 Unknown, 0032 ParsingMetaData, 0033 ParsingIngredients 0034 }; 0035 0036 RecipeParser::ParsedRecipe RecipeParser::parseRecipe(QIODevice* input) 0037 { 0038 if (!input) { 0039 qDebug() << "no input"; 0040 return {}; 0041 } 0042 if (!input->isReadable()) { 0043 return {}; 0044 } 0045 0046 RecipeParser::ParsedRecipe result; 0047 auto state = State::Unknown; 0048 while (!input->atEnd()) { 0049 QByteArray line = input->readLine(2000); 0050 if (line.startsWith("# ")) { 0051 result.title = QString::fromUtf8(line.right(line.length()-2)).trimmed(); 0052 continue; 0053 } 0054 if (line.startsWith("### Ingred")) { 0055 state = State::ParsingIngredients; 0056 continue; 0057 } 0058 if (line.startsWith("### Direc")) { 0059 state = State::Unknown; 0060 continue; 0061 } 0062 if (line.startsWith("### Meta")) { 0063 state = State::ParsingMetaData; 0064 continue; 0065 } 0066 switch(state) 0067 { 0068 case State::ParsingIngredients: 0069 { 0070 auto ingredient = IngredientsExtractor::parseLine(QString::fromUtf8(line)); 0071 if (!ingredient.unit.isEmpty() && !ingredient.ingredient.isEmpty()) { 0072 result.ingredients.push_back(std::move(ingredient)); 0073 } 0074 continue; 0075 } 0076 case State::ParsingMetaData: 0077 { 0078 int sep = line.indexOf(':'); 0079 if (sep != -1) { 0080 QString key = QString::fromUtf8(line.left(sep)).trimmed(); 0081 QString value = QString::fromUtf8(line.mid(sep+1)).trimmed(); 0082 if (!value.isEmpty()) 0083 { 0084 if (key == QLatin1String("tags")) { 0085 QStringList tags = value.split(',',Qt::SkipEmptyParts); 0086 for(QString tag : qAsConst(tags)) { 0087 result.tags.push_back(tag.trimmed()); 0088 } 0089 } else { 0090 result.otherMeta[key].push_back(value); 0091 } 0092 } 0093 } 0094 continue; 0095 } 0096 case State::Unknown: 0097 continue; 0098 } 0099 0100 } 0101 0102 return result; 0103 }