Re: Embedding inputlang as information

Lorenzo Bettini <[email protected]> Sat, 23 Apr 2011 18:03:53 +0200
Newsgroups gmane.comp.gnu.source-highlight.general
Message-ID <[email protected]>
Hi

your patch (the original one, which is in this email) is not in the git 
repository.

this patch breaks binary compatibility so I increased the version number 
(which will also change the soname).

it will be part of the next release

many thanks
	Lorenzo

On 03/02/2011 08:49 PM, Masatake YAMATO wrote:
> Hi,
>
> I'd like you to review my patch.
> The patch embeds input lang used in highlighting into the output xhtml
> file.
>
> Background
> -----------
> I'm developing web based source code viewer using source highlight.
>
> With javascript I'd like to provide some interactive commands working
> on the generated html file. The behavior of commands will be changed
> according to the ``input lang'' of the original source code.
>
> e.g.:
>
> JUMP command works on
>
>       #include<bar.h>
>
> in C,
>
>       import foo.bar.Class;
>
> in Java, and
>
>       import foo.bar.Class
>
> in python...
>
>
> If source highlight embeds the name of input lang to the converted html file,
> I don't have to write lang detector in javascript.  Adding this feature is a
> bit private request but similar type of software may want such
> input lang(file type) information. So I submitted the patch here.
>
>
> I don't know how I have to be careful about API and ABI compatibility.
> In the patch I may do something bad about the compatibility.
>
>
> Masatake YAMATO
>
>
> diff --git a/lib/srchilite/docgenerator.cc b/lib/srchilite/docgenerator.cc
> index 1e1ae73..6494e6f 100644
> --- a/lib/srchilite/docgenerator.cc
> +++ b/lib/srchilite/docgenerator.cc
> @@ -31,6 +31,7 @@ void
>   DocGenerator::generate_start_doc(std::ostream *sout)
>   {
>       bool docTitle = (title.size ()>  0);
> +    bool docInputLang = (input_lang.size ()>  0);
>       bool inputFileName = (input_file_name.size ()>  0);
>
>       *sout<<
> @@ -42,7 +43,8 @@ DocGenerator::generate_start_doc(std::ostream *sout)
>               "\nby Lorenzo Bettini\nhttp://www.lorenzobettini.it\nhttp://www.gnu.org/software/src-highlite",
>               doc_header,
>               doc_footer,
> -            doc_background
> +            doc_background,
> +	    docInputLang ? input_lang: "unknown"
>           );
>   }
>
> @@ -56,7 +58,8 @@ DocGenerator::generate_end_doc(std::ostream *sout)
>               "\nby Lorenzo Bettini\nhttp://www.lorenzobettini.it\nhttp://www.gnu.org/software/src-highlite",
>               doc_header,
>               doc_footer,
> -            doc_background
> +            doc_background,
> +	    ""
>           );
>   }
>
> diff --git a/lib/srchilite/docgenerator.h b/lib/srchilite/docgenerator.h
> index 16a2804..3a6282d 100644
> --- a/lib/srchilite/docgenerator.h
> +++ b/lib/srchilite/docgenerator.h
> @@ -47,16 +47,18 @@ protected:
>       string css_url;
>       string doc_background;
>       bool entire_doc;
> +    string input_lang;
>
>       DocTemplate docTemplate;
>
>   public:
>       DocGenerator(const string&s, const string&i, const string&h,
> -            const string&f, const string&c, const string&back, bool entire,
> +            const string&f, const string&c, const string&back, bool entire,
> +	    const string&l,
>               const string&start_tmpl, const string&end_tmpl) :
>           title(s), gen_source_highlight_version(true), input_file_name(i),
>                   doc_header(h), doc_footer(f), css_url(c), doc_background(back),
> -                entire_doc(entire), docTemplate(DocTemplate(start_tmpl,
> +	  entire_doc(entire), input_lang(l), docTemplate(DocTemplate(start_tmpl,
>                           end_tmpl)) {
>       }
>       DocGenerator(const string&start_tmpl, const string&end_tmpl) :
> @@ -97,6 +99,10 @@ public:
>           title = _title;
>       }
>
> +    void setInputLang(const std::string&_input_lang) {
> +      input_lang = _input_lang;
> +    }
> +
>       void setBackgroundColor(const std::string&bg) {
>           doc_background = bg;
>       }
> diff --git a/lib/srchilite/doctemplate.cpp b/lib/srchilite/doctemplate.cpp
> index 32b4e2f..63309c5 100644
> --- a/lib/srchilite/doctemplate.cpp
> +++ b/lib/srchilite/doctemplate.cpp
> @@ -14,12 +14,14 @@
>   namespace srchilite {
>
>   #define TITLE_VAR_TEXT "$title" // the text of the title variable
> +#define INPUT_LANG_VAR_TEXT "$inputlang" // the text of the input lang variable
>   #define CSS_VAR_TEXT "$css" // the text of the css variable
>   #define ADDITIONAL_VAR_TEXT "$additional" // the text of the additional text variable
>   #define HEADER_VAR_TEXT "$header" // the text of the header variable
>   #define FOOTER_VAR_TEXT "$footer" // the text of the footer variable
>   #define BACKGROUND_VAR_TEXT "$docbgcolor" // the text of the background variable
>   #define TITLE_VAR "\\" TITLE_VAR_TEXT // the name of the title variable as regexp
> +#define INPUT_LANG_VAR "\\" INPUT_LANG_VAR_TEXT // the name of the input lang variable as regexp
>   #define CSS_VAR "\\" CSS_VAR_TEXT // the name of the css variable as regexp
>   #define ADDITIONAL_VAR "\\" ADDITIONAL_VAR_TEXT // the text of the additional text variable as regexp
>   #define HEADER_VAR "\\" HEADER_VAR_TEXT // the text of the header variable as regexp
> @@ -35,7 +37,8 @@ DocTemplate::DocTemplate(const string&begin, const string&end) :
>
>   string
>   DocTemplate::output_begin(const string&title, const string&cs, const string&add,
> -    const string&header, const string&footer, const std::string&background)
> +    const string&header, const string&footer, const std::string&background,
> +    const string&input_lang)
>   {
>       boost::regex title_exp(TITLE_VAR);
>       boost::regex css_exp(CSS_VAR);
> @@ -43,6 +46,7 @@ DocTemplate::output_begin(const string&title, const string&cs, const string&a
>       boost::regex header_exp(HEADER_VAR);
>       boost::regex footer_exp(FOOTER_VAR);
>       boost::regex background_exp(BACKGROUND_VAR);
> +    boost::regex input_lang_exp(INPUT_LANG_VAR);
>
>       string ret = subst(title_exp, begin_repr, title);
>       ret = subst(css_exp, ret, cs);
> @@ -50,13 +54,15 @@ DocTemplate::output_begin(const string&title, const string&cs, const string&a
>       ret = subst(header_exp, ret, header);
>       ret = subst(footer_exp, ret, footer);
>       ret = subst(background_exp, ret, background);
> +    ret = subst(input_lang_exp, ret, input_lang);
>
>       return ret;
>   }
>
>   string
>   DocTemplate::output_end(const string&title, const string&cs, const string&add,
> -    const string&header, const string&footer, const std::string&background)
> +    const string&header, const string&footer, const std::string&background,
> +    const string&input_lang)
>   {
>       boost::regex title_exp(TITLE_VAR);
>       boost::regex css_exp(CSS_VAR);
> @@ -64,6 +70,7 @@ DocTemplate::output_end(const string&title, const string&cs, const string&add
>       boost::regex header_exp(HEADER_VAR);
>       boost::regex footer_exp(FOOTER_VAR);
>       boost::regex background_exp(BACKGROUND_VAR);
> +    boost::regex input_lang_exp(INPUT_LANG_VAR);
>
>       string ret = subst(title_exp, end_repr, title);
>       ret = subst(css_exp, ret, cs);
> @@ -71,6 +78,7 @@ DocTemplate::output_end(const string&title, const string&cs, const string&add
>       ret = subst(header_exp, ret, header);
>       ret = subst(footer_exp, ret, footer);
>       ret = subst(background_exp, ret, background);
> +    ret = subst(input_lang_exp, ret, input_lang);
>
>       return ret;
>   }
> diff --git a/lib/srchilite/doctemplate.h b/lib/srchilite/doctemplate.h
> index 7ee0e1a..17340e6 100644
> --- a/lib/srchilite/doctemplate.h
> +++ b/lib/srchilite/doctemplate.h
> @@ -22,10 +22,12 @@ public:
>
>       std::string output_begin(const std::string&title, const std::string&cs,
>               const std::string&add, const std::string&header,
> -            const std::string&footer, const std::string&background);
> +            const std::string&footer, const std::string&background,
> +	    const std::string&input_lang);
>       std::string output_end(const std::string&title, const std::string&cs,
>               const std::string&add, const std::string&header,
> -            const std::string&footer, const std::string&background);
> +            const std::string&footer, const std::string&background,
> +	    const std::string&input_lang);
>
>       const std::string&toStringBegin() const {
>           return begin_repr;
> diff --git a/lib/srchilite/sourcehighlight.cpp b/lib/srchilite/sourcehighlight.cpp
> index 9c0a55b..3abaed7 100644
> --- a/lib/srchilite/sourcehighlight.cpp
> +++ b/lib/srchilite/sourcehighlight.cpp
> @@ -257,6 +257,9 @@ void SourceHighlight::highlight(const std::string&input,
>           noDocGenerator->setTitle(input);
>       }
>
> +    docGenerator->setInputLang(inputLang);
> +    noDocGenerator->setInputLang(inputLang);
> +
>       if (ctagsFormatter) {
>           // if we need to generate references, then set the file info
>           ctagsFormatter->setFileInfo(input, output);
> @@ -334,6 +337,8 @@ void SourceHighlight::highlight(std::istream&input, std::ostream&output,
>           documentGenerator->setTitle(title);
>       }
>
> +    documentGenerator->setInputLang(inputLang);
> +
>       // first generate the start of the output file
>       documentGenerator->generate_start_doc(&output);
>
> diff --git a/lib/srchilite/sourcehighlight.h b/lib/srchilite/sourcehighlight.h
> index 7d61972..4cef31e 100644
> --- a/lib/srchilite/sourcehighlight.h
> +++ b/lib/srchilite/sourcehighlight.h
> @@ -64,6 +64,9 @@ class SourceHighlight {
>       /// the title for the output document (defaults to the source file name)
>       std::string title;
>
> +    /// the input lang for the output document
> +    std::string inputLang;
> +
>       /// the value for the css
>       std::string css;
>
> @@ -272,6 +275,10 @@ public:
>       void setTitle(const std::string&_title) {
>           title = _title;
>       }
> +
> +    void setInputLang(const std::string&_inputLang) {
> +        inputLang = _inputLang;
> +    }
>
>       void setCss(const std::string&_css) {
>           css = _css;
> diff --git a/lib/tests/test_outlangparser_main.cpp b/lib/tests/test_outlangparser_main.cpp
> index 467604b..d7f51cf 100644
> --- a/lib/tests/test_outlangparser_main.cpp
> +++ b/lib/tests/test_outlangparser_main.cpp
> @@ -45,7 +45,7 @@ this is simply the footer: $footer",
>               textstyles->docTemplate.toStringEnd());
>
>       string start = textstyles->docTemplate.output_begin("title", "css",
> -            "additional", "header", "footer", "");
> +            "additional", "header", "footer", "", "INPUTLANG");
>       cout<<  "DocTemplate start:\n"<<  start<<  endl;
>
>       assertEquals(
> @@ -59,7 +59,7 @@ and this is some additional stuff: additional",
>               start);
>
>       string end = textstyles->docTemplate.output_end("title", "css",
> -            "additional", "header", "footer", "");
> +            "additional", "header", "footer", "", "INPUTLANG");
>       cout<<  "DocTemplate end:\n"<<  end<<  endl;
>
>       assertEquals(
> diff --git a/lib/tests/test_textgenerator_main.cpp b/lib/tests/test_textgenerator_main.cpp
> index aeaef3b..ed58660 100644
> --- a/lib/tests/test_textgenerator_main.cpp
> +++ b/lib/tests/test_textgenerator_main.cpp
> @@ -74,9 +74,9 @@ int main() {
>       DocTemplate docTemplate(start, end);
>
>       string transformed_start = docTemplate.output_begin("TITLE", "CSS",
> -            "ADDITIONAL", "HEADER\n", "\nFOOTER", "");
> +            "ADDITIONAL", "HEADER\n", "\nFOOTER", "", "INPUTLANG");
>       string transformed_end = docTemplate.output_end("TITLE", "CSS",
> -            "ADDITIONAL", "HEADER\n", "\nFOOTER", "");
> +            "ADDITIONAL", "HEADER\n", "\nFOOTER", "", "INPUTLANG");
>
>       cout<<  "orig start : "<<  start<<  endl;
>       cout<<  "transformed: "<<  transformed_start<<  endl;
> diff --git a/src/xhtml.outlang b/src/xhtml.outlang
> index ecc1a27..cd5d02c 100644
> --- a/src/xhtml.outlang
> +++ b/src/xhtml.outlang
> @@ -7,6 +7,7 @@ doctemplate
>   <head>
>   <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />
>   <meta name=\"GENERATOR\" content=\"$additional\" />
> +<meta name=\"INPUTLANG\" content=\"$inputlang\" />
>   <title>$title</title>
>   </head>
>   <body style=\"background-color: $docbgcolor\">
> diff --git a/src/xhtmlcss.outlang b/src/xhtmlcss.outlang
> index 215800f..1b1387f 100644
> --- a/src/xhtmlcss.outlang
> +++ b/src/xhtmlcss.outlang
> @@ -7,6 +7,7 @@ doctemplate
>   <head>
>   <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />
>   <meta name=\"GENERATOR\" content=\"$additional\" />
> +<meta name=\"INPUTLANG\" content=\"$inputlang\" />
>   <title>$title</title>
>   <link rel=\"stylesheet\" href=\"$css\" type=\"text/css\" />
>   </head>
>
> _______________________________________________
> Help-source-highlight mailing list
> [email protected]
> http://lists.gnu.org/mailman/listinfo/help-source-highlight


-- 
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
ICQ# lbetto, 16080134     (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
http://www.myspace.com/supertrouperabba
BLOGS: http://tronprog.blogspot.com  http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net