summaryrefslogtreecommitdiffstats
path: root/krecipes/src/exporters/rezkonvexporter.cpp
blob: e71489713b8b74a2d8d7858ffb0e5fff7d784622 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/***************************************************************************
*   Copyright (C) 2005 by                                                 *
*   Jason Kivlighn (jkivlighn@gmail.com)                                  *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
***************************************************************************/

#include "rezkonvexporter.h"

#include <qregexp.h>

#include <kconfig.h>
#include <kdebug.h>
#include <klocale.h>
#include <kapplication.h>

#include "backends/recipedb.h"
#include "datablocks/mixednumber.h"

struct translate_unit_info
{
	const char *english;
	const char *english_plural;
	const char *german;
	const char *german_plural;
};

static translate_unit_info translate_units[] = {
                                          {"g", "g", "Gramm", "Gramms" },
                                          {"ml", "ml", "ml", "ml"},
                                          {"l", "l", "Ltr.", "Ltr."},
                                          {"kg", "kg", "kg", "kg"},
                                          {"mg", "mg", "mg", "mg"},
                                          {"teaspoon", "teaspoons", "Teel.", "Teel."},
                                          {"t.", "t.", "Teel.", "Teel."},
                                          {"t", "t", "Teel.", "Teel."},
                                          {"tsp.", "tsp.", "Teel.", "Teel."},
                                          {"tsp", "tsp", "Teel.", "Teel."},
                                          {"tablespoon", "tablespoons", "Essl.", "Essl."},
                                          {"tbsp.", "tbsp.", "Essl.", "Essl."},
                                          {"tbsp", "tbsp", "Essl.", "Essl."},
                                          {"T", "T", "Essl.", "Essl."},
                                          {"T.", "T.", "Essl.", "Essl."},
                                          {"cup", "cups", "Tasse", "Tassen"},
                                          {"c.", "c.", "Tasse", "Tassen"},
                                          {"can", "cans", "Dose", "Dosen"},
                                          {"drop", "drops", "Tropfen", "Tropfen"},
                                          {"large", "large", "groß", "groß"},
                                          {"medium", "medium", "mittl.", "mittl."},
                                          {"small", "small", "klein.", "klein."},
                                          {"pinch", "pinches", "Prise", "Prisen"},
                                          {"package", "packages", "Pack.", "Pack."},
                                          {"bunch", "bunches", "Bund.", "Bunde"},
                                          {"stem", "stems", "Strange", "Strangen"},
                                          {"twig", "twigs", "Zweig", "Zweige"},
                                          {"tip of a knife", "tips of a knife", "Messersp.", "Messersp."},
                                          {"sheet", "sheets", "Blatt", "Blätter"},
                                          {"handful", "handfuls", "Handvoll", "Handvoll"},
                                          {"head", "heads", "Kopf", "Köpfe"},
                                          {"slice", "slices", "Scheibe", "Sheiben"},
                                          {"some", "some", "Einige", "Einige"},
                                          {"a little", "a little", "Etwas", "Etwas"},
                                          {"little can", "little cans", "Döschen", "Döschen"},
                                          {"glass", "glasses", "Glas", "Gläser"},
                                          {"piece", "pieces", "Stück", "Stück"},
                                          {"pot", "pots", "Topf", "Topf"},
                                          {"generous", "generous", "Reichlich", "Reichlich"},
                                          {"dash", "dashes", "Spritzer", "Spritzer"},
                                          {"clove", "cloves", "Zehe", "Zehen"},
                                          {"slice", "slices", "Platte", "Platten"},
                                          {"shot", "shots", "Schuss", "Schuss"},
                                          {"peduncle", "peduncles", "Stiel", "Stiele"},
                                          {"heaping teaspoon", "heaping teaspoons", "geh. TL", "geh. TL"},
                                          {"heaping tsp.", "heaping tsp.", "geh. TL", "geh. TL"},
                                          {"heaping tsp.", "heaping tsp.", "geh. TL", "geh. TL"},
                                          {"heaping tablespoon", "heaping tablespoon", "geh. EL", "geh. EL"},
                                          {"heaping tbsp.", "heaping tbsp.", "geh. EL", "geh. EL"},
#if 0
                                          {"pound", "pounds", "", ""},
                                          {"lb.", "lbs.", "", ""},
                                          {"ounce", "ounces", "", ""},
                                          {"oz.", "oz.", "", ""},
#endif
                                          {"", "", "", ""},
                                          { 0, 0, 0, 0 }
                                      };

RezkonvExporter::RezkonvExporter( const QString& filename, const QString& format ) :
		BaseExporter( filename, format )
{}


RezkonvExporter::~RezkonvExporter()
{}

int RezkonvExporter::supportedItems() const
{
	return RecipeDB::All ^ RecipeDB::Photo ^ RecipeDB::Ratings;
}

QString RezkonvExporter::createContent( const RecipeList& recipes )
{
	QString content;

	RecipeList::const_iterator recipe_it;
	for ( recipe_it = recipes.begin(); recipe_it != recipes.end(); ++recipe_it ) {
		writeHeader( content, *recipe_it );
		content += "\n";
		writeIngredients( content, *recipe_it );
		content += "\n";
		writeDirections( content, *recipe_it );
		content += "\n";

		content += "=====\n\n"; //end of recipe indicator
	}

	return content;
}

/* Header:
 * Line 1 - contains five hyphens and "Meal-Master" somewhere in the line
 * Line 2 - "Title:" followed by a blank space; maximum of 60 char
 * Line 3 - "Categories:" followed by a blank space; Maximum of 5
 * Line 4 - Numeric quantity representing the # of servings (1-9999)
 */
void RezkonvExporter::writeHeader( QString &content, const Recipe &recipe )
{
	content += QString( "===== Exported by Krecipes v%1 [REZKONV Export Format] =====\n\n" ).arg( krecipes_version() );

	QString title = recipe.title;
	title.truncate( 60 );
	content += QString("Titel: ").rightJustify( 13, ' ', true) + title + "\n";

	int i = 0;
	QStringList categories;
	for ( ElementList::const_iterator cat_it = recipe.categoryList.begin(); cat_it != recipe.categoryList.end(); ++cat_it ) {
		i++;

		if ( i == 6 )
			break; //maximum of 5 categories
		categories << ( *cat_it ).name;
	}
	QString cat_str = QString("Kategorien: ").rightJustify( 13, ' ', true) + categories.join( ", " );
	cat_str.truncate( 67 );
	content += cat_str + "\n";

	content += QString("Menge: ").rightJustify( 13, ' ', true) + recipe.yield.toString() + "\n";
}

/* Ingredient lines:
 * Positions 1-7 contains a numeric quantity
 * Positions 9-10 Meal-Master unit of measure codes
 * Positions 12-39 contain text for an ingredient name, or a "-"
 *   in position 12 and text in positions 13-39 (the latter is a
 *   "continuation" line for the previous ingredient name)
 */
void RezkonvExporter::writeIngredients( QString &content, const Recipe &recipe )
{
	//this format requires ingredients without a group to be written first
	for ( IngredientList::const_iterator ing_it = recipe.ingList.begin(); ing_it != recipe.ingList.end(); ++ing_it ) {
		if ( ( *ing_it ).groupID == -1 ) {
			writeSingleIngredient( content, *ing_it, (*ing_it).substitutes.count() > 0 );

			for ( QValueList<IngredientData>::const_iterator sub_it = (*ing_it).substitutes.begin(); sub_it != (*ing_it).substitutes.end(); ) {
				QValueList<IngredientData>::const_iterator save_it = sub_it;

				 ++sub_it;
				writeSingleIngredient( content, *save_it, sub_it != (*ing_it).substitutes.end() );
			}
		}
	}

	IngredientList list_copy = recipe.ingList;
	for ( IngredientList group_list = list_copy.firstGroup(); group_list.count() != 0; group_list = list_copy.nextGroup() ) {
		if ( group_list[ 0 ].groupID == -1 )  //we already handled this group
			continue;

		QString group = group_list[ 0 ].group.left( 76 ); //just use the first's name... they're all the same
		if ( !group.isEmpty() ) {
			int length = group.length();
			QString filler_lt = QString().fill( '=', ( 76 - length ) / 2 );
			QString filler_rt = ( length % 2 ) ? QString().fill( '=', ( 76 - length ) / 2 + 1 ) : filler_lt;
			content += filler_lt + group + filler_rt + "\n";
		}

		for ( IngredientList::const_iterator ing_it = group_list.begin(); ing_it != group_list.end(); ++ing_it ) {
			writeSingleIngredient( content, *ing_it, (*ing_it).substitutes.count() > 0 );

			for ( QValueList<IngredientData>::const_iterator sub_it = (*ing_it).substitutes.begin(); sub_it != (*ing_it).substitutes.end(); ) {
				QValueList<IngredientData>::const_iterator save_it = sub_it;

				 ++sub_it;
				writeSingleIngredient( content, *save_it, sub_it != (*ing_it).substitutes.end() );
			}
		}
	}

	QString authorLines;
	if ( recipe.authorList.count() > 0 ) {
		content += "============================== QUELLE ==============================\n";
		authorLines = "                   "+(*recipe.authorList.begin()).name+"\n";
	}
	for ( ElementList::const_iterator author_it = ++recipe.authorList.begin(); author_it != recipe.authorList.end(); ++author_it ) {
		authorLines += "                   -- ";
		authorLines += (*author_it).name + "\n";
	}
	if ( !authorLines.isEmpty() )
		authorLines += "\n";
	content += authorLines;
}

void RezkonvExporter::writeSingleIngredient( QString &content, const IngredientData &ing, bool is_sub )
{
	KConfig * config = kapp->config();
	config->setGroup( "Formatting" );
	MixedNumber::Format number_format = ( config->readBoolEntry( "Fraction" ) ) ? MixedNumber::MixedNumberFormat : MixedNumber::DecimalFormat;

	//columns 1-7
	if ( ing.amount > 1e-8 ) {
		QString amount_str = MixedNumber( ing.amount ).toString( number_format, false );

		if ( ing.amount_offset > 0 )
			amount_str += "-"+MixedNumber( ing.amount + ing.amount_offset ).toString( number_format, false );

		if ( amount_str.length() > 7 ) { //too long, let's try the other formatting
			MixedNumber::Format other_format = (number_format == MixedNumber::DecimalFormat) ? MixedNumber::MixedNumberFormat : MixedNumber::DecimalFormat;

			QString new_amount_str = MixedNumber( ing.amount ).toString( other_format, false );
	
			if ( ing.amount_offset > 0 )
				new_amount_str += "-"+MixedNumber( ing.amount + ing.amount_offset ).toString( other_format, false );

			if (new_amount_str.length() > 7) { //still too long, use original formatting, but truncate it
				amount_str = amount_str.left(7);
				kdDebug()<<"Warning: Amount text too long, truncating"<<endl;
			}
		}
		content += amount_str.rightJustify( 7, ' ', true ) + " ";
	}
	else
		content += QString().fill(' ',7+1);

	//columns 9-19
	bool found_translation = false;
	for ( int i = 0; translate_units[ i ].english; i++ ) {
		if ( translate_units[ i ].english == ing.units.name ||  translate_units[ i ].english_plural == ing.units.plural ||  translate_units[ i ].german == ing.units.name ||  translate_units[ i ].german_plural == ing.units.plural ) {
			found_translation = true;
			QString unit;
			if ( ing.amount > 1 )
				unit += translate_units[i].german;
			else
				unit += translate_units[i].german_plural;
			content += unit.leftJustify( 9 ) + " ";
			break;
		}
	}
	if ( !found_translation ) {
		kdDebug() << "Warning: unable to find German translation for: " << ing.units.name << endl;
		kdDebug() << "         This ingredient (" << ing.name << ") will be exported without a unit" << endl;
		content += QString().fill(' ',9+1);
	}

	//columns 21-70
	QString ing_name( ing.name );
	if ( ing.prepMethodList.count() > 0 )
		ing_name += "; " + ing.prepMethodList.join(", ");

	if ( is_sub )
		ing_name += ", or"; //FIXME: what's 'or' in German?

	if ( !found_translation )
		ing_name.prepend( ( ing.amount > 1 ? ing.units.plural : ing.units.name ) + " " );

	//try and split the ingredient on a word boundry
	int split_index;
	if ( ing_name.length() > 50 ) {
		split_index = ing_name.left(50).findRev(" ")+1;
		if ( split_index == 0 )
			split_index = 50;
	}
	else
		split_index = 50;

	content += ing_name.left(split_index) + "\n";

	for ( unsigned int i = 0; i < ( ing_name.length() - 1 ) / 50; i++ )  //if longer than 50 chars, continue on next line(s)
		content += QString().fill(' ',(7+1)+(9+1)) + "-" + ing_name.mid( 50 * ( i ) + split_index, 50 ) + "\n";
}

void RezkonvExporter::writeDirections( QString &content, const Recipe &recipe )
{
	QStringList lines = QStringList::split("\n",recipe.instructions,true);
	for ( QStringList::const_iterator it = lines.begin(); it != lines.end(); ++it ) {
		content += wrapText( *it, 80 ).join( "\n" ) + "\n";
	}
}

QStringList RezkonvExporter::wrapText( const QString& str, int at ) const
{
	QStringList ret;
	QString copy( str );
	bool stop = false;
	while ( !stop ) {
		QString line( copy.left( at ) );
		if ( line.length() >= copy.length() )
			stop = true;
		else {
			QRegExp rxp( "(\\s\\S*)$", false ); // last word in the new line
			rxp.setMinimal( true );    // one word, only one word, please
			line = line.replace( rxp, "" ); // remove last word
		}
		copy = copy.remove( 0, line.length() );
		line = line.stripWhiteSpace();
		line.prepend( " " );       // indent line by one char
		ret << line; // output of current line
	}

	return ret;
}