/// <summary> /// Parses a script from the specified resource. /// </summary> /// <param name="context">The <see cref="IMansionContext"/>.</param> /// <param name="resource">The resource wcich to parse as script.</param> /// <returns>Returns the parsed script.</returns> /// <exception cref="ParseScriptException">Thrown when an exception occurres while parsing the script.</exception> public IExpressionScript Parse(IMansionContext context, IResource resource) { // validate arguments if (context == null) throw new ArgumentNullException("context"); if (resource == null) throw new ArgumentNullException("resource"); // open the script var cacheKey = ResourceCacheKey.Create(resource); return cachingService.GetOrAdd( context, cacheKey, () => { // get the phrase string rawPhrase; using (var resourceStream = resource.OpenForReading()) using (var reader = resourceStream.Reader) rawPhrase = reader.ReadToEnd(); // let someone else do the heavy lifting var phrase = ParsePhrase(context, rawPhrase); // create the cache object return new CachedPhrase(phrase); }); }
/// <summary> /// Opens a template. /// </summary> /// <param name="context">The <see cref="IMansionContext"/>.</param> /// <param name="resource">The resource which to open.</param> /// <returns>Returns a marker which will close the template automatically.</returns> public IDisposable Open(IMansionContext context, IResource resource) { // validate arguments if (context == null) throw new ArgumentNullException("context"); if (resource == null) throw new ArgumentNullException("resource"); // open the template var cacheKey = ResourceCacheKey.Create(resource); var template = cachingService.GetOrAdd( context, cacheKey, () => { // read the complete template into memory string rawTemplate; using (var resourceStream = resource.OpenForReading()) using (var reader = resourceStream.Reader) rawTemplate = reader.ReadToEnd(); // loop through all the sections var sections = (from rawSection in sectionTokenizer.Tokenize(context, rawTemplate) let interpreter = Election<SectionInterpreter, string>.Elect(context, interpreters, rawSection) select interpreter.Interpret(context, rawSection) ).ToList(); // create the template var htmlTemplate = new Template(sections, resource.Path); // create the cached template return new CachedHtmlTemplate(htmlTemplate); }); // push the template to the stack return context.TemplateStack.Push(template); }