Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StyleSheet Replacement #5

Open
corxit opened this issue Aug 30, 2019 · 0 comments
Open

StyleSheet Replacement #5

corxit opened this issue Aug 30, 2019 · 0 comments

Comments

@corxit
Copy link

corxit commented Aug 30, 2019

I added some code into mdict-parser.js and posted a pull-request months ago. This is a better method below.

Usage

replace this line: return harvest(list.map(findWord));
with: return harvest(list.map(findWord)).map(replaceStyleMark);;

    function replaceStyleMark(content_string){

        /**
         * Replace Mark to CsssStyle
         * Processing With: https://www.douban.com/note/526161004/ - 法汉汉法词典
         * Niubility Dictionary Ultimate v1.0
         */

        var content_with_style = "";

        //check if has special mark in content
        let content_style_keys = content_string.match(/`\d+`/g);
        if (content_style_keys == null || content_style_keys.length <= 0){
            content_with_style = content_string;
        }
        else {

            //check if has stylesheet in mdx
            let styles_string = attrs['StyleSheet'];
            if (styles_string !== undefined && styles_string != null) {

                //make styles
                let styles_string_adjuested_space = (" " + styles_string.replace(/^\s*/g, ''));
                let style_strings = styles_string_adjuested_space.split(/ \d+ /g).filter(function (style) {
                    return style.replace(/\s/g, '') !== "";
                });

                var styles = {};
                $.each(style_strings, function (index, value) {
                    let style_key = "`"+(index+1)+"`";
                    styles[style_key] = value;
                });

                //make content breaks
                $.each(content_style_keys, function (index, style_key) {
                    content_string = content_string.replace(new RegExp(style_key, 'g'), "<break>" + style_key);
                });

                //loop content breaks
                $.each(content_string.split("<break>").filter(function (line) {
                    return line.replace(/\s/g, '') !== '';
                }), function (line_index, line) {

                  //loop styles
                  $.each(styles, function (key, style) {
                    if (line.includes(key)){
                      let style_templates = style.match(new RegExp('<[^>]*>(\\s)+</[^>]*>', 'g'));
                        if (style_templates !== null && style_templates.length === 1) {
                          let template = style_templates[0];
                          let line_content = line.replace(new RegExp(key, 'g'), '');
                          let style_with_line_content = style.replace(
                              template,
                              template.replace(/>(\s)+</g, ">" + line_content + "<"));
                          line = style_with_line_content;
                        } else {
                          line = line.replace(new RegExp(key, 'g'), style);
                        }
                    }
                  });

                  content_with_style = content_with_style + line
                });
            }

            //replace invalid content_style_key with <br>
            content_with_style = content_with_style.replace(/`\d+`/g, "<br>");
        }

        return content_with_style;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant