From 3d3beb39602113ba02d587a25dd6deea912c5bbe Mon Sep 17 00:00:00 2001 From: "Fletcher T. Penney" Date: Fri, 20 Jan 2017 13:53:22 -0500 Subject: [PATCH] ADDED: German translations; command line option to select language --- src/html.c | 11 +++++------ src/i18n.h | 30 ++++++++++++++++++++++++------ src/libMultiMarkdown.h | 8 ++++++-- src/main.c | 22 +++++++++++++++++----- src/mmd.c | 24 ++++++++++++++++++++++++ src/mmd.h | 3 +++ src/writer.c | 3 ++- src/writer.h | 6 +++++- 8 files changed, 86 insertions(+), 21 deletions(-) diff --git a/src/html.c b/src/html.c index 37f758a..35b9cd7 100644 --- a/src/html.c +++ b/src/html.c @@ -114,8 +114,7 @@ void mmd_print_string_html(DString * out, const char * str, bool obfuscate) { void mmd_print_localized_char_html(DString * out, unsigned short type, scratch_pad * scratch) { - // TODO: Is smart typography enabled? Which language? - int language = 0; + // TODO: Is smart typography enabled? switch (type) { case DASH_N: @@ -131,7 +130,7 @@ void mmd_print_localized_char_html(DString * out, unsigned short type, scratch_p print("’"); break; case QUOTE_LEFT_SINGLE: - switch (language) { + switch (scratch->quotes_lang) { case SWEDISH: print( "’"); break; @@ -149,7 +148,7 @@ void mmd_print_localized_char_html(DString * out, unsigned short type, scratch_p } break; case QUOTE_RIGHT_SINGLE: - switch (language) { + switch (scratch->quotes_lang) { case GERMAN: print("‘"); break; @@ -161,7 +160,7 @@ void mmd_print_localized_char_html(DString * out, unsigned short type, scratch_p } break; case QUOTE_LEFT_DOUBLE: - switch (language) { + switch (scratch->quotes_lang) { case DUTCH: case GERMAN: print("„"); @@ -180,7 +179,7 @@ void mmd_print_localized_char_html(DString * out, unsigned short type, scratch_p } break; case QUOTE_RIGHT_DOUBLE: - switch (language) { + switch (scratch->quotes_lang) { case GERMAN: print("“"); break; diff --git a/src/i18n.h b/src/i18n.h index a6f09af..1a489df 100644 --- a/src/i18n.h +++ b/src/i18n.h @@ -29,7 +29,7 @@ #include -#define kNumberOfLanguages 2 +#define kNumberOfLanguages 3 #define kNumberOfStrings 3 #define kLanguage 0 @@ -42,6 +42,7 @@ #ifdef I18N_DISABLED #define LC(x) x + #define LANG_FROM_STR(x) 0 #else #define H1(s,i,x) (x*65599u+(uint8_t)s[(i)>16))) - #define LC(x) Translate(HASH(x), kLanguage) + #define LC(x) Translate(HASH(x), scratch->language) //#define LC(x) TranslateTest(__COUNTER__, __FILE__, __LINE__, __FUNCTION__ , x) + #define LANG_FROM_STR(x) i18n_language_from_string(x) + static const char * lc_lookup[kNumberOfLanguages * kNumberOfStrings] = { "return to body", // English - "return to body", // Spanish + "return to body", // Español + "zum Haupttext", // Deutsch "see footnote", // English - "Ver nota de pie", // Spanish + "Ver nota de pie", // Español + "siehe Fußnote", // Deutsch "see citation", // English - "see citation", // Spanish + "see citation", // Español + "siehe Zitat", // Deutsch }; @@ -93,10 +99,22 @@ static inline const char * Translate(unsigned long x, int l) { // https://en.wikipedia.org/wiki/ISO_639-1 enum lc_languages { LC_EN = 0, //!< English is default - LC_ES, + LC_ES, //!< Español + LC_DE, //!< Deutsch }; +static inline short i18n_language_from_string(const char * l) { + if (strcmp(l, "de") == 0) { + return LC_DE; + } else if (strcmp(l, "es") == 0) { + return LC_ES; + } + + return 0; +} + + #endif #endif diff --git a/src/libMultiMarkdown.h b/src/libMultiMarkdown.h index a0bcfe8..9da0f82 100644 --- a/src/libMultiMarkdown.h +++ b/src/libMultiMarkdown.h @@ -101,6 +101,10 @@ void mmd_engine_parse_string(mmd_engine * e); void mmd_export_token_tree(DString * out, mmd_engine * e, short format); +/// Set language and smart quotes language +void mmd_engine_set_language(mmd_engine * e, short language); + + /// Token types for parse tree enum token_types { DOC_START_TOKEN = 0, //!< DOC_START_TOKEN must be type 0 @@ -255,8 +259,8 @@ enum token_types { /// Define smart typography languages -- first in list is default -enum smart_language { - ENGLISH, +enum smart_quotes_language { + ENGLISH = 0, DUTCH, FRENCH, GERMAN, diff --git a/src/main.c b/src/main.c index 1ad3e8f..9d4211f 100644 --- a/src/main.c +++ b/src/main.c @@ -61,6 +61,7 @@ #include "argtable3.h" #include "d_string.h" +#include "i18n.h" #include "libMultiMarkdown.h" #include "html.h" #include "mmd.h" @@ -71,10 +72,10 @@ // argtable structs struct arg_lit *a_help, *a_version, *a_compatibility, *a_nolabels, *a_batch, *a_accept, *a_reject; -struct arg_str *a_format; +struct arg_str *a_format, *a_lang; struct arg_file *a_file, *a_o; struct arg_end *a_end; -struct arg_rem *a_rem1, *a_rem2, *a_rem3; +struct arg_rem *a_rem1, *a_rem2, *a_rem3, *a_rem4; DString * stdin_buffer() { @@ -151,11 +152,13 @@ char * filename_with_extension(const char * original, const char * new_extension } -char * mmd_process(DString * buffer, unsigned long extensions, short format) { +char * mmd_process(DString * buffer, unsigned long extensions, short format, short language) { char * result; mmd_engine * e = mmd_engine_create_with_dstring(buffer, extensions); + mmd_engine_set_language(e, language); + mmd_engine_parse_string(e); DString * output = d_string_new(""); @@ -175,6 +178,7 @@ int main(int argc, char** argv) { int exitcode = EXIT_SUCCESS; char * binname = "multimarkdown"; short format = 0; + short language = LC_EN; // Initialize argtable structs void *argtable[] = { @@ -199,6 +203,10 @@ int main(int argc, char** argv) { a_nolabels = arg_lit0(NULL, "nolabels", "Disable id attributes for headers"), a_file = arg_filen(NULL, NULL, "", 0, argc+2, "read input from file(s)"), + + a_rem4 = arg_rem("", ""), + + a_lang = arg_str0("l", "lang", "LANG", "language localization, LANG = en|es|de"), a_end = arg_end(20), }; @@ -276,6 +284,10 @@ int main(int argc, char** argv) { } } + if (a_lang->count > 0) { + language = LANG_FROM_STR(a_lang->sval[0]); + } + // Determine input if (a_file->count == 0) { // Read from stdin @@ -314,7 +326,7 @@ int main(int argc, char** argv) { break; } - result = mmd_process(buffer, extensions, format); + result = mmd_process(buffer, extensions, format, language); if (!(output_stream = fopen(output_filename, "w"))) { // Failed to open file @@ -354,7 +366,7 @@ int main(int argc, char** argv) { buffer = stdin_buffer(); } - result = mmd_process(buffer, extensions, format); + result = mmd_process(buffer, extensions, format, language); // Where does output go? if (strcmp(a_o->filename[0], "-") == 0) { diff --git a/src/mmd.c b/src/mmd.c index d94fe08..30689bd 100644 --- a/src/mmd.c +++ b/src/mmd.c @@ -59,6 +59,7 @@ #include "char.h" #include "d_string.h" #include "html.h" /// \todo: Remove this for production +#include "i18n.h" #include "lexer.h" #include "libMultiMarkdown.h" #include "mmd.h" @@ -90,6 +91,9 @@ mmd_engine * mmd_engine_create(DString * d, unsigned long extensions) { e->extensions = extensions; + e->language = LC_EN; + e->quotes_lang = ENGLISH; + e->citation_stack = stack_new(0); e->definition_stack = stack_new(0); e->footnote_stack = stack_new(0); @@ -165,6 +169,26 @@ mmd_engine * mmd_engine_create_with_string(const char * str, unsigned long exten } +/// Set language and smart quotes language +void mmd_engine_set_language(mmd_engine * e, short language) { + e->language = language; + + switch (language) { + case LC_EN: + e->quotes_lang = ENGLISH; + break; + case LC_DE: + e->quotes_lang = GERMAN; + break; + case LC_ES: + e->quotes_lang = ENGLISH; + break; + default: + e->quotes_lang = ENGLISH; + } +} + + /// Free an existing MMD Engine void mmd_engine_free(mmd_engine * e, bool freeDString) { if (e == NULL) diff --git a/src/mmd.h b/src/mmd.h index f44ee82..61733d7 100644 --- a/src/mmd.h +++ b/src/mmd.h @@ -76,6 +76,9 @@ struct mmd_engine { stack * header_stack; stack * footnote_stack; stack * link_stack; + + short language; + short quotes_lang; }; diff --git a/src/writer.c b/src/writer.c index 5c7e3de..4b229e2 100644 --- a/src/writer.c +++ b/src/writer.c @@ -125,8 +125,9 @@ scratch_pad * scratch_pad_new(mmd_engine * e) { } - p->extensions = e->extensions; + p->quotes_lang = e->quotes_lang; + p->language = e->language; } return p; diff --git a/src/writer.h b/src/writer.h index 83f4829..db288b3 100644 --- a/src/writer.h +++ b/src/writer.h @@ -86,7 +86,11 @@ typedef struct { struct fn_holder * citation_hash; short citation_being_printed; - char _PADDING[6]; //!< pad struct for alignment + short language; + short quotes_lang; + + char _PADDING[4]; //!< pad struct for alignment + } scratch_pad; -- 2.40.0