diff --git a/README.md b/README.md index 5057b34..d3dd38e 100644 --- a/README.md +++ b/README.md @@ -403,6 +403,16 @@ Saves the passed GIF as an ascii art GIF with the name `-ascii-art.g

+#### --save-html + +Similar to --save-img but it creates a HTML file with the name `-ascii-art.html` in the directory path passed to the flag. Can save colored text, that can be copied from open html file. + +Example for current directory: + +``` +ascii-image-converter [image paths/urls] --save-html . +``` + #### --save-bg > **Note:** This flag will be ignored if `--save-img` or `--save-gif` flags are not set @@ -550,4 +560,4 @@ You can fork the project and implement any changes you want for a pull request. ## License -[Apache-2.0](https://github.com/TheZoraiz/ascii-image-converter/blob/master/LICENSE.txt) +[Apache-2.0](https://github.com/TheZoraiz/ascii-image-converter/blob/master/LICENSE.txt) \ No newline at end of file diff --git a/aic_package/convert_image.go b/aic_package/convert_image.go index 6c09fd1..4c573eb 100644 --- a/aic_package/convert_image.go +++ b/aic_package/convert_image.go @@ -94,6 +94,22 @@ func pathIsImage(imagePath, urlImgName string, pathIsURl bool, urlImgBytes, pipe } } + // Save ascii art as .html file before printing it, if --save-html flag is passed + if saveHtmlPath != "" { + if err := createHtmlToSave( + asciiSet, + colored, + saveHtmlPath, + imagePath, + urlImgName, + saveBgColor, + onlySave, + ); err != nil { + + return "", fmt.Errorf("can't save file: %v", err) + } + } + ascii := flattenAscii(asciiSet, colored || grayscale, false) result := strings.Join(ascii, "\n") diff --git a/aic_package/convert_root.go b/aic_package/convert_root.go index c4a8782..d7fd3f1 100644 --- a/aic_package/convert_root.go +++ b/aic_package/convert_root.go @@ -54,6 +54,7 @@ func DefaultFlags() Flags { SaveTxtPath: "", SaveImagePath: "", SaveGifPath: "", + SaveHtmlPath: "", Negative: false, Colored: false, CharBackgroundColor: false, @@ -90,6 +91,7 @@ func Convert(filePath string, flags Flags) (string, error) { saveTxtPath = flags.SaveTxtPath saveImagePath = flags.SaveImagePath saveGifPath = flags.SaveGifPath + saveHtmlPath = flags.SaveHtmlPath negative = flags.Negative colored = flags.Colored colorBg = flags.CharBackgroundColor diff --git a/aic_package/create_ascii_html.go b/aic_package/create_ascii_html.go new file mode 100644 index 0000000..4b1d99e --- /dev/null +++ b/aic_package/create_ascii_html.go @@ -0,0 +1,102 @@ +/* +Copyright © 2021 Zoraiz Hassan + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package aic_package + +import ( + "fmt" + "os" + "strings" + + imgManip "github.com/TheZoraiz/ascii-image-converter/image_manipulation" +) + +/* +Creates and saves an HTML file containing the passed ascii art. The HTML file will contain the ascii art as spans with inline color style if [colored] is true +*/ +func createHtmlToSave(asciiArt [][]imgManip.AsciiChar, colored bool, saveHtmlPath, imagePath, urlImgName string, saveBgColor [4]int, onlySave bool) error { + var builder strings.Builder + + backgroundColor := fmt.Sprintf("#%02x%02x%02x", saveBgColor[0], saveBgColor[1], saveBgColor[2]) + + // Start the HTML file with the necessary headers and styles + builder.WriteString(fmt.Sprintf(` + + + + ASCII Art + +
`, backgroundColor))
+
+	for _, line := range asciiArt {
+		for _, asciiChar := range line {
+			// Extract the RGB values from the RgbValue field
+			rgb := asciiChar.RgbValue
+			// Convert the character and color into an HTML span
+			if colored {
+				builder.WriteString(fmt.Sprintf(
+					"%s",
+					rgb[0], rgb[1], rgb[2], asciiChar.Simple,
+				))
+			} else {
+				builder.WriteString(fmt.Sprintf(
+					"%s",
+					asciiChar.Simple,
+				))
+			}
+		}
+		// Add a line break after each line
+		builder.WriteString("
") + } + + // End the HTML document + builder.WriteString(`
`) + + + saveFileName, err := createSaveFileName(imagePath, urlImgName, "-ascii-art.html") + if err != nil { + return err + } + + savePathLastChar := string(saveHtmlPath[len(saveHtmlPath)-1]) + + // Check if path is closed with appropriate path separator (depending on OS) + if savePathLastChar != string(os.PathSeparator) { + saveHtmlPath += string(os.PathSeparator) + } + + // If path exists + if _, err := os.Stat(saveHtmlPath); !os.IsNotExist(err) { + err := os.WriteFile(saveHtmlPath+saveFileName, []byte(builder.String()), 0666) + if err != nil { + return err + } else if onlySave { + fmt.Println("Saved " + saveHtmlPath + saveFileName) + } + return nil + } else { + return fmt.Errorf("save path %v does not exist", saveHtmlPath) + } +} diff --git a/aic_package/vars.go b/aic_package/vars.go index feb0451..ec35a84 100644 --- a/aic_package/vars.go +++ b/aic_package/vars.go @@ -39,6 +39,9 @@ type Flags struct { // Path to save ascii art .png file SaveImagePath string + // Path to save ascii art .html file + SaveHtmlPath string + // Path to save ascii art .gif file, if gif is passed SaveGifPath string @@ -111,6 +114,7 @@ var ( complex bool saveTxtPath string saveImagePath string + saveHtmlPath string saveGifPath string grayscale bool negative bool diff --git a/cmd/root.go b/cmd/root.go index 55277e1..571e994 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -36,6 +36,7 @@ var ( height int saveTxtPath string saveImagePath string + saveHtmlPath string saveGifPath string negative bool formatsTrue bool @@ -76,6 +77,7 @@ var ( SaveTxtPath: saveTxtPath, SaveImagePath: saveImagePath, SaveGifPath: saveGifPath, + SaveHtmlPath: saveHtmlPath, Negative: negative, Colored: colored, CharBackgroundColor: colorBg, @@ -161,7 +163,8 @@ func init() { rootCmd.PersistentFlags().StringVarP(&saveImagePath, "save-img", "s", "", "Save ascii art as a .png file\nFormat: -ascii-art.png\nImage will be saved in passed path\n(pass . for current directory)\n") rootCmd.PersistentFlags().StringVar(&saveTxtPath, "save-txt", "", "Save ascii art as a .txt file\nFormat: -ascii-art.txt\nFile will be saved in passed path\n(pass . for current directory)\n") rootCmd.PersistentFlags().StringVar(&saveGifPath, "save-gif", "", "If input is a gif, save it as a .gif file\nFormat: -ascii-art.gif\nGif will be saved in passed path\n(pass . for current directory)\n") - rootCmd.PersistentFlags().IntSliceVar(&saveBgColor, "save-bg", nil, "Set background color for --save-img\nand --save-gif flags\nPass an RGBA value\ne.g. --save-bg 255,255,255,100\n(Defaults to 0,0,0,100)\n") + rootCmd.PersistentFlags().StringVar(&saveHtmlPath, "save-html", "", "Save ascii art as a .html file\nFormat: -ascii-art.html\nFile will be saved in passed path\n(pass . for current directory)\n") + rootCmd.PersistentFlags().IntSliceVar(&saveBgColor, "save-bg", nil, "Set background color for --save-img\nand --save-gif \nand --save-html flags\nPass an RGBA value\ne.g. --save-bg 255,255,255,100\n(Defaults to 0,0,0,100)\n") rootCmd.PersistentFlags().StringVar(&fontFile, "font", "", "Set font for --save-img and --save-gif flags\nPass file path to font .ttf file\ne.g. --font ./RobotoMono-Regular.ttf\n(Defaults to Hack-Regular for ascii and\n DejaVuSans-Oblique for braille)\n") rootCmd.PersistentFlags().IntSliceVar(&fontColor, "font-color", nil, "Set font color for terminal as well as\n--save-img and --save-gif flags\nPass an RGB value\ne.g. --font-color 0,0,0\n(Defaults to 255,255,255)\n") rootCmd.PersistentFlags().BoolVar(&onlySave, "only-save", false, "Don't print ascii art on terminal\nif some saving flag is passed\n") diff --git a/cmd/util.go b/cmd/util.go index 7bc174d..eed8916 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -172,8 +172,8 @@ func checkInputAndFlags(args []string) bool { return true } - if (saveTxtPath == "" && saveImagePath == "" && saveGifPath == "") && onlySave { - fmt.Printf("Error: you need to supply one of --save-img, --save-txt or --save-gif for using --only-save\n\n") + if (saveTxtPath == "" && saveImagePath == "" && saveGifPath == "" && saveHtmlPath == "") && onlySave { + fmt.Printf("Error: you need to supply one of --save-img, --save-txt, --save-gif or --save-html for using --only-save\n\n") return true }