Пример #1
0
	public static void Main(string[] args){
		InitConsole();

		string srcfile=checkArgument(args);
		if(srcfile==null)return;

		FileInfo info=new FileInfo();
		info.filename=_args.option_filename??srcfile;
		System.Console.WriteLine("file '{0}' を処理します",srcfile);
		string content=System.IO.File.ReadAllText(srcfile,enc);

		content=Preprocessor.Process(content,info);
    content=RegExp.JsConvertAtStrings(content); // @"" @'' の処理
		content=TranslateContext.Translate(_args,content);
		if(_args.option_compress)
			content=RegExp.CutComment(content);
		else
			content=RegExp.CanonicalizeLine(content);

    if(!_args.option_partial){
      content=string.Format(
        FRAME,
        info.filename,
        enc.WebName,
        System.DateTime.Now,
        srcfile,
        content,
        info.GetMwgScriptsRequirement()
      );
    }
		System.IO.File.WriteAllText(info.filename+".js",content);

		System.Console.WriteLine("file '{0}.js' に書き込みました",info.filename);
	}
Пример #2
0
    public static string ProcessSource(string input, Argument _args)
    {
        string   output = input;
        Argument args   = _args;

        // directives の読み取り
        Gen::Dictionary <string, string> tokenmap = new Gen::Dictionary <string, string>();
        Gen::List <ReplaceData>          replaces = new Gen::List <ReplaceData>();

        output = reg_gzjs_directive.Replace(output, delegate(Rgx::Match m) {
            // 先頭以外に改行が入っている物は無効
            if (0 < m.Value.IndexOfAny("\r\n".ToCharArray()))
            {
                return(m.Value);
            }

            switch (m.Groups["dir"].Value)
            {
            case "outfile": {
                string dirname  = System.IO.Path.GetDirectoryName(args.OutputFile);
                args.OutputFile = System.IO.Path.Combine(dirname, ReadArg(m.Groups["arg1"]));
                if (args.Verbose)
                {
                    args.WriteLine("#> output-file was set to '" + args.OutputFile + "'");
                }
                return("\n");
            }

            case "tokenmap": {
                string before    = ReadArg(m.Groups["arg1"]);
                string after     = ReadArg(m.Groups["arg2"]);
                tokenmap[before] = after;
                if (args.Verbose)
                {
                    args.WriteLine("#> token-mapping registered: " + before + " -> " + after + "");
                }
                return("\n");
            }

            case "replace": {
                string before = ReadArg(m.Groups["arg1"]);
                string after  = ReadArg(m.Groups["arg2"]);
                replaces.Add(new ReplaceData(before, after));
                if (args.Verbose)
                {
                    args.WriteLine("#> replace registered: " + before + " -> " + after + "");
                }
                return("\n");
            }

            case "include": {
                if (args.Verbose)
                {
                    args.WriteLine("#gzjs-include");
                }
                return(Program.Include(ReadArg(m.Groups["arg1"]), args) ?? m.Value);
            }

            case "option": {
                string op = ReadArg(m.Groups["arg1"]);
                if (!args.ReadOption(op))
                {
                    args.WriteLine("#gzjs-option > '{0}' は認識できない option です", op);
                    return(m.Value);
                }
                return("\n");
            }

            default:
                return(m.Value);
            }
        });

        // コメント空白類の削除
        if (args.CutComment)
        {
            output = RegExp.CutComment(output);
        }

        // token 置き換え
        if (tokenmap.Count > 0)
        {
            string rex = null;
            foreach (string token in tokenmap.Keys)
            {
                if (rex == null)
                {
                    rex = token;
                }
                else
                {
                    rex += "|" + token;
                }
            }
            rex    = @"\b(?:" + rex + @")\b";
            output = new Rgx::Regex(rex).Replace(output, delegate(Rgx::Match m) {
                return(tokenmap[m.Value]);
            });
        }

        // #gzjs-replace 実行
        foreach (ReplaceData r in replaces)
        {
            output = r.Replace(output);
        }

        return(output);
    }