+
+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
}