public void NUglify() { var settings = new NuSettings() { MinifyCss = false, MinifyJs = false }; string minifiedContent = NuMinifier.Html(s_documents[DocumentName].Content, settings).Code; }
/// <summary> /// 返回压缩后的html /// </summary> /// <param name="source">原始html</param> /// <returns></returns> internal static string Html(string source) { var htmlsetting = new NUglify.Html.HtmlSettings { // 此项如果不设为false,压缩后将丢失</body></html>两标签 RemoveOptionalTags = false, // 标签上的属性值为空时不要删除,例如<input value=""> RemoveEmptyAttributes = false }; var result = Uglify.Html(source, htmlsetting); return(result.Code); }
public static string ProcessRequest(HtmlMinify req) { var initialBody = req.html; var settings = new NUglify.Html.HtmlSettings { RemoveOptionalTags = false, RemoveEmptyAttributes = false, RemoveAttributeQuotes = false, DecodeEntityCharacters = true, RemoveScriptStyleTypeAttribute = false, ShortBooleanAttribute = false, MinifyJs = false, MinifyJsAttributes = false, MinifyCss = false, MinifyCssAttributes = false }; var minifiedBody = Uglify.Html(initialBody, settings); return(minifiedBody.Code); }
private void ProcessMessageAsync(SQSEvent.SQSMessage message, ILambdaContext context) { var api = new APIHelper(); try { var log = context.Logger; System.Diagnostics.Stopwatch transpileStopWatch = new System.Diagnostics.Stopwatch(); System.Diagnostics.Stopwatch completeProcessStopWatch = new System.Diagnostics.Stopwatch(); completeProcessStopWatch.Start(); //Default threshold if not provided is set to 250 sec var functionThreshold = System.Environment.GetEnvironmentVariable("FunctionThreshold") ?? "250000"; if (message != null && !string.IsNullOrEmpty(message.Body)) { var request = JsonConvert.DeserializeObject <JObject>(message.Body); if (request == null) { log.LogLine("Error : Request body can not be empty"); return; } var projectId = request["ProjectId"] != null ? (string)request["ProjectId"] : null; var userEmail = request["UserEmail"] != null ? (string)request["UserEmail"] : null; var resourceProcessed = request["ResourcesProcesed"] != null ? (int)request["ResourcesProcesed"] : 0; if (string.IsNullOrEmpty(projectId) || string.IsNullOrEmpty(userEmail)) { log.LogLine($"Invalid request paramater : {message.Body}"); return; } try { log.LogLine($"Transpiling the project : {projectId}"); log.LogLine($"Getting project details for project : {projectId}"); var project = api.GetProjectDetailsApi(userEmail, projectId); var totalFiles = 0; var completedFiles = resourceProcessed; if (project != null && project.Resources != null && project.Resources.Any(x => !x.IsStatic)) { totalFiles = project.Resources.Count(x => !x.IsStatic && x.ResourceType == Models.Project.ResourceType.LINK); KitsunePage page = null; log.LogLine($"Transpiling '{project.Resources.Count(x => !x.IsStatic) - resourceProcessed}' resources "); string compiledFile = string.Empty; foreach (var resource in project.Resources.Where(x => !x.IsStatic && x.ResourceType == Models.Project.ResourceType.LINK).OrderByDescending(x => x.UpdatedOn).Skip(resourceProcessed)) { transpileStopWatch.Reset(); transpileStopWatch.Start(); try { compiledFile = api.GetFileFromS3(new GetFileFromS3RequestModel { BucketName = project.BucketNames.demo, Compiled = true, ProjectId = project.ProjectId, SourcePath = resource.SourcePath }); } catch (Exception ex) { log.LogLine($"Exception : {ex.Message}, StackTrace : {ex.StackTrace}"); return; } if (!string.IsNullOrEmpty(compiledFile)) { try { NUglify.Html.HtmlSettings settings = new NUglify.Html.HtmlSettings() { DecodeEntityCharacters = false, RemoveOptionalTags = false, ShortBooleanAttribute = false }; //settings.TagsWithNonCollapsableWhitespaces.Add("p", false); NUglify.UglifyResult minify = NUglify.Uglify.Html(compiledFile, settings); if (!minify.HasErrors) { page = new NodeProcessor().Process(minify.Code, resource.SourcePath, resource.KObject, resource.CustomVariables, resource.Offset); } else { string errorList = ""; foreach (NUglify.UglifyError error in minify.Errors) { errorList += $"[{error.StartLine},{error.StartColumn}:{error.EndLine},{error.EndColumn}]{error.ErrorCode}:{error.ErrorNumber}:{error.Message};"; } log.LogLine($"Error : File minification for '{resource.SourcePath}', erorrList {errorList}"); //Fallback page = new NodeProcessor().Process(minify.Code, resource.SourcePath, resource.KObject, resource.CustomVariables, resource.Offset); } } catch (Exception ex) { log.LogLine($"Exception : File minification for '{resource.SourcePath}', Message : {ex.Message}, StackTrace : {ex.StackTrace}"); //Fallback page = new NodeProcessor().Process(compiledFile, resource.SourcePath, resource.KObject, resource.CustomVariables, resource.Offset); } } if (page != null) { var jsonSerializeSettings = new JsonSerializerSettings(); jsonSerializeSettings.TypeNameHandling = TypeNameHandling.Auto; string output = JsonConvert.SerializeObject(page, Formatting.None, jsonSerializeSettings); var resourceContent = new SaveFileContentToS3RequestModel { ProjectId = project.ProjectId, BucketName = project.BucketNames.demo, SourcePath = $"{resource.SourcePath}.kc", FileContent = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(output)), base64 = true }; var compiledStringResult = api.SaveFileContentToS3(resourceContent); if (compiledStringResult == null) { } //Increase the completed files count completedFiles++; } else { //Update project and build error UpdateBuildError(project.ProjectId, project.UserEmail, resource.SourcePath, context); break; } log.LogLine($"{completedFiles}/{totalFiles} '{resource.SourcePath}' {transpileStopWatch.ElapsedMilliseconds.ToString()}"); //Check if the runtime is exceeding the timelimit then push to queue with completed task. if (completedFiles < totalFiles && completeProcessStopWatch.ElapsedMilliseconds > int.Parse(functionThreshold)) { var msgBody = new { ProjectId = projectId, UserEmail = userEmail, ResourcesProcesed = completedFiles }; var response = AWSSQSHelper.PushMessageToQueue(msgBody, context, RegionEndpoint.GetBySystemName(message.AwsRegion)); if (!string.IsNullOrEmpty(response)) { log.LogLine($"Pushed the message to queue : {JsonConvert.SerializeObject(msgBody)}"); } //Exit the loop; break; } } } else { totalFiles = completedFiles = 0; log.LogLine("No resource found to transpile"); } if (completedFiles >= totalFiles) { log.LogLine($"Transpilation done successfully for project '{project.ProjectId}' in {completeProcessStopWatch.ElapsedMilliseconds} ms"); log.LogLine("Updating Build Status"); var buildStatusUpdateResult = api.UpdateBuildStatus(project.UserEmail, new CreateOrUpdateKitsuneStatusRequestModel { ProjectId = project.ProjectId, Stage = BuildStatus.Completed, }); log.LogLine("Success : Updating Build Status"); log.LogLine("Calling trigger completed event"); if (api.TriggerCompletedEvent(project.ProjectId).IsError) { log.LogLine($"Error updating transpilation complete status for project '{project.ProjectId}'"); } log.LogLine("Success : Calling trigger completed event"); } } catch (Exception ex) { UpdateBuildError(projectId, userEmail, null, context); if (api.TriggerFailedEvent(projectId).IsError) { log.LogLine($"Error updating transpilation failed status for project '{projectId}'"); } throw; } // TODO: Do interesting work based on the new message } } catch { throw; } finally { AWSSQSHelper.DeleteMessageFromQueue(message, context); } }