✨ added support for language, topic, and preset acquisition and detection functions
This commit is contained in:
218
examples/09-supported-options/main.go
Normal file
218
examples/09-supported-options/main.go
Normal file
@@ -0,0 +1,218 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/landaiqing/freezelib"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("📋 FreezeLib Supported Options Examples")
|
||||
fmt.Println("=======================================")
|
||||
|
||||
// Create output directory
|
||||
err := os.MkdirAll("output", 0755)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error creating output directory: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Run examples
|
||||
languageListExample()
|
||||
themeListExample()
|
||||
presetListExample()
|
||||
generateExamples()
|
||||
}
|
||||
|
||||
// Language list example
|
||||
func languageListExample() {
|
||||
fmt.Println("\n💻 Supported Languages")
|
||||
fmt.Println("---------------------")
|
||||
|
||||
freeze := freezelib.New()
|
||||
|
||||
// Get all supported languages
|
||||
allLanguages := freeze.GetSupportedLanguages()
|
||||
fmt.Printf("📈 Total supported languages: %d\n", len(allLanguages))
|
||||
|
||||
// Show first 20 languages as examples
|
||||
fmt.Println("\n📋 First 20 supported languages:")
|
||||
for i, lang := range allLanguages[:min(20, len(allLanguages))] {
|
||||
fmt.Printf(" %2d. %s\n", i+1, lang)
|
||||
}
|
||||
|
||||
// Test language support
|
||||
testLanguages := []string{"go", "python", "javascript", "rust", "unknown-lang"}
|
||||
fmt.Println("\n🧪 Testing language support:")
|
||||
for _, lang := range testLanguages {
|
||||
supported := freeze.IsLanguageSupported(lang)
|
||||
status := "❌"
|
||||
if supported {
|
||||
status = "✅"
|
||||
}
|
||||
fmt.Printf(" %s %s\n", status, lang)
|
||||
}
|
||||
}
|
||||
|
||||
// Theme list example
|
||||
func themeListExample() {
|
||||
fmt.Println("\n🎨 Supported Themes")
|
||||
fmt.Println("------------------")
|
||||
|
||||
freeze := freezelib.New()
|
||||
|
||||
// Get all supported themes
|
||||
allThemes := freeze.GetSupportedThemes()
|
||||
fmt.Printf("📈 Total supported themes: %d\n", len(allThemes))
|
||||
|
||||
// Show first 20 themes as examples
|
||||
fmt.Println("\n📋 First 20 supported themes:")
|
||||
for i, theme := range allThemes[:min(20, len(allThemes))] {
|
||||
fmt.Printf(" %2d. %s\n", i+1, theme)
|
||||
}
|
||||
|
||||
// Test theme support
|
||||
testThemes := []string{"github", "dracula", "monokai", "unknown-theme"}
|
||||
fmt.Println("\n🧪 Testing theme support:")
|
||||
for _, theme := range testThemes {
|
||||
supported := freeze.IsThemeSupported(theme)
|
||||
status := "❌"
|
||||
if supported {
|
||||
status = "✅"
|
||||
}
|
||||
fmt.Printf(" %s %s\n", status, theme)
|
||||
}
|
||||
}
|
||||
|
||||
// Preset list example
|
||||
func presetListExample() {
|
||||
fmt.Println("\n⚙️ Available Presets")
|
||||
fmt.Println("-------------------")
|
||||
|
||||
freeze := freezelib.New()
|
||||
|
||||
// Get all available presets
|
||||
presets := freeze.GetAvailablePresets()
|
||||
fmt.Printf("📈 Total available presets: %d\n", len(presets))
|
||||
|
||||
// Show all presets
|
||||
fmt.Println("\n📋 Available presets:")
|
||||
for i, preset := range presets {
|
||||
fmt.Printf(" %2d. %s\n", i+1, preset)
|
||||
}
|
||||
|
||||
// Test preset validity
|
||||
testPresets := []string{"base", "full", "terminal", "dark", "light", "unknown-preset"}
|
||||
fmt.Println("\n🧪 Testing preset validity:")
|
||||
for _, preset := range testPresets {
|
||||
valid := freezelib.IsValidPreset(preset)
|
||||
status := "❌"
|
||||
if valid {
|
||||
status = "✅"
|
||||
}
|
||||
fmt.Printf(" %s %s\n", status, preset)
|
||||
}
|
||||
}
|
||||
|
||||
// Generate examples with different options
|
||||
func generateExamples() {
|
||||
fmt.Println("\n🎨 Generating Examples")
|
||||
fmt.Println("---------------------")
|
||||
|
||||
freeze := freezelib.New()
|
||||
|
||||
// Sample code for examples
|
||||
sampleCode := `package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello, World!")
|
||||
|
||||
// This is a comment
|
||||
for i := 0; i < 10; i++ {
|
||||
fmt.Printf("Count: %d\n", i)
|
||||
}
|
||||
}`
|
||||
|
||||
// Generate examples with different themes
|
||||
themes := []string{"github", "github-dark", "dracula", "monokai"}
|
||||
for _, theme := range themes {
|
||||
if !freeze.IsThemeSupported(theme) {
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("🎨 Generating example with %s theme...\n", theme)
|
||||
|
||||
svgData, err := freeze.WithTheme(theme).
|
||||
WithFont("JetBrains Mono", 14).
|
||||
WithWindow(true).
|
||||
WithLineNumbers(true).
|
||||
WithShadow(15, 0, 8).
|
||||
WithPadding(20).
|
||||
GenerateFromCode(sampleCode, "go")
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error with theme %s: %v\n", theme, err)
|
||||
continue
|
||||
}
|
||||
|
||||
filename := fmt.Sprintf("output/theme_%s.svg", theme)
|
||||
err = os.WriteFile(filename, svgData, 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error saving %s: %v\n", filename, err)
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("✅ Generated: %s\n", filename)
|
||||
}
|
||||
|
||||
// Generate examples with different presets
|
||||
presets := []string{"base", "full", "terminal", "dark", "light"}
|
||||
bashCode := `#!/bin/bash
|
||||
|
||||
echo "Starting deployment..."
|
||||
|
||||
if [ ! -d "dist" ]; then
|
||||
echo "Building project..."
|
||||
npm run build
|
||||
fi
|
||||
|
||||
echo "Deploying to server..."
|
||||
rsync -av dist/ user@server:/var/www/html/
|
||||
|
||||
echo "Deployment complete!"`
|
||||
|
||||
for _, preset := range presets {
|
||||
if !freezelib.IsValidPreset(preset) {
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("⚙️ Generating example with %s preset...\n", preset)
|
||||
|
||||
presetFreeze := freezelib.NewWithPreset(preset)
|
||||
svgData, err := presetFreeze.GenerateFromCode(bashCode, "bash")
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error with preset %s: %v\n", preset, err)
|
||||
continue
|
||||
}
|
||||
|
||||
filename := fmt.Sprintf("output/preset_%s.svg", preset)
|
||||
err = os.WriteFile(filename, svgData, 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error saving %s: %v\n", filename, err)
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("✅ Generated: %s\n", filename)
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
@@ -42,6 +42,19 @@ This directory contains comprehensive examples demonstrating various features of
|
||||
- Automated workflows
|
||||
- Bulk operations
|
||||
|
||||
### [08-auto-language-detection/](08-auto-language-detection/) - Auto Language Detection
|
||||
- Content-based language detection
|
||||
- Filename-based detection
|
||||
- Custom detector configuration
|
||||
- Detection API usage
|
||||
|
||||
### [09-supported-options/](09-supported-options/) - Supported Options
|
||||
- Get all supported languages list
|
||||
- Get all supported themes list
|
||||
- Get all available presets list
|
||||
- Validate language, theme, preset support
|
||||
- Simple and efficient API
|
||||
|
||||
## 🚀 Quick Start
|
||||
|
||||
To run all examples:
|
||||
@@ -54,54 +67,19 @@ go run run_all_examples.go
|
||||
To run specific category:
|
||||
|
||||
```bash
|
||||
# Basic usage
|
||||
cd examples/01-basic
|
||||
go run main.go
|
||||
|
||||
# Auto language detection
|
||||
cd examples/08-auto-language-detection
|
||||
go run main.go
|
||||
|
||||
# Supported options
|
||||
cd examples/09-supported-options
|
||||
go run main.go
|
||||
```
|
||||
|
||||
## 📊 Output Formats
|
||||
|
||||
Each example category demonstrates:
|
||||
- **SVG**: Vector format, perfect for web and documentation
|
||||
- **PNG**: Raster format, ideal for presentations and social media
|
||||
- **Quality comparisons**: Different settings and their effects
|
||||
|
||||
## 🎨 Visual Features Demonstrated
|
||||
|
||||
- **Syntax Highlighting**: 100+ programming languages
|
||||
- **Themes**: Light, dark, and custom themes
|
||||
- **Window Decorations**: macOS-style window controls
|
||||
- **Line Numbers**: Optional line numbering
|
||||
- **Shadows and Borders**: Visual enhancement effects
|
||||
- **Custom Fonts**: Typography options
|
||||
- **ANSI Colors**: Terminal output rendering
|
||||
- **Responsive Sizing**: Adaptive dimensions
|
||||
|
||||
## 📝 Code Examples Include
|
||||
|
||||
- **Web Development**: HTML, CSS, JavaScript, TypeScript
|
||||
- **Backend**: Go, Python, Java, C#, Rust
|
||||
- **Mobile**: Swift, Kotlin, Dart
|
||||
- **DevOps**: Docker, YAML, Shell scripts
|
||||
- **Data**: SQL, JSON, CSV processing
|
||||
- **Documentation**: Markdown, configuration files
|
||||
|
||||
## 🔧 Configuration Examples
|
||||
|
||||
Each category includes examples of:
|
||||
- Basic configuration
|
||||
- Advanced customization
|
||||
- Performance optimization
|
||||
- Error handling
|
||||
- Best practices
|
||||
|
||||
## 📖 Learning Path
|
||||
|
||||
1. **Start with [01-basic/](01-basic/)** - Learn fundamental concepts
|
||||
2. **Explore [02-formats/](02-formats/)** - Understand output options
|
||||
3. **Try [03-themes/](03-themes/)** - Discover visual styles
|
||||
4. **Check [04-languages/](04-languages/)** - See language support
|
||||
5. **Advanced topics** - Dive into specialized use cases
|
||||
|
||||
## 🤝 Contributing Examples
|
||||
|
||||
To add new examples:
|
||||
|
@@ -42,6 +42,20 @@
|
||||
- 自动化工作流
|
||||
- 批量操作
|
||||
|
||||
### [08-auto-language-detection/](08-auto-language-detection/) - 自动语言检测
|
||||
- 智能语言检测
|
||||
- 内容分析检测
|
||||
- 文件名检测
|
||||
- 自定义检测器配置
|
||||
- 检测 API 使用
|
||||
|
||||
### [09-supported-options/](09-supported-options/) - 支持选项列表
|
||||
- 获取所有支持的语言列表
|
||||
- 获取所有支持的主题列表
|
||||
- 获取所有可用的预设列表
|
||||
- 验证语言、主题、预设支持
|
||||
- 简单高效的 API
|
||||
|
||||
## 🚀 快速开始
|
||||
|
||||
运行所有示例:
|
||||
@@ -54,53 +68,20 @@ go run run_all_examples.go
|
||||
运行特定分类:
|
||||
|
||||
```bash
|
||||
# 基础示例
|
||||
cd examples/01-basic
|
||||
go run main.go
|
||||
|
||||
# 自动语言检测
|
||||
cd examples/08-auto-language-detection
|
||||
go run main.go
|
||||
|
||||
# 支持选项列表
|
||||
cd examples/09-supported-options
|
||||
go run main.go
|
||||
```
|
||||
|
||||
## 📊 输出格式
|
||||
|
||||
每个示例分类都演示:
|
||||
- **SVG**: 矢量格式,完美适用于网页和文档
|
||||
- **PNG**: 栅格格式,适合演示和社交媒体
|
||||
- **质量对比**: 不同设置及其效果
|
||||
|
||||
## 🎨 展示的视觉功能
|
||||
|
||||
- **语法高亮**: 100+ 种编程语言
|
||||
- **主题**: 浅色、深色和自定义主题
|
||||
- **窗口装饰**: macOS 风格窗口控件
|
||||
- **行号**: 可选行号显示
|
||||
- **阴影和边框**: 视觉增强效果
|
||||
- **自定义字体**: 排版选项
|
||||
- **ANSI 颜色**: 终端输出渲染
|
||||
- **响应式尺寸**: 自适应尺寸
|
||||
|
||||
## 📝 代码示例包括
|
||||
|
||||
- **Web 开发**: HTML, CSS, JavaScript, TypeScript
|
||||
- **后端**: Go, Python, Java, C#, Rust
|
||||
- **移动端**: Swift, Kotlin, Dart
|
||||
- **DevOps**: Docker, YAML, Shell 脚本
|
||||
- **数据**: SQL, JSON, CSV 处理
|
||||
- **文档**: Markdown, 配置文件
|
||||
|
||||
## 🔧 配置示例
|
||||
|
||||
每个分类都包含以下示例:
|
||||
- 基本配置
|
||||
- 高级自定义
|
||||
- 性能优化
|
||||
- 错误处理
|
||||
- 最佳实践
|
||||
|
||||
## 📖 学习路径
|
||||
|
||||
1. **从 [01-basic/](01-basic/) 开始** - 学习基本概念
|
||||
2. **探索 [02-formats/](02-formats/)** - 了解输出选项
|
||||
3. **尝试 [03-themes/](03-themes/)** - 发现视觉样式
|
||||
4. **查看 [04-languages/](04-languages/)** - 了解语言支持
|
||||
5. **高级主题** - 深入专业用例
|
||||
|
||||
## 🤝 贡献示例
|
||||
|
||||
|
Reference in New Issue
Block a user