@@ -28,6 +28,96 @@ func (s *Section) addListItem(item string) {
2828 s .addLine (line ) // Append the formatted list item as a new line.
2929}
3030
31+ // addBulletItem formats and adds a bullet point item to the Section.
32+ func (s * Section ) addBulletItem (item string ) {
33+ line := fmt .Sprintf ("- %s" , item ) // Format the item with a bullet point.
34+ s .addLine (line ) // Append the formatted bullet item as a new line.
35+ }
36+
37+ // addCodeBlock formats and adds a code block with optional language specification.
38+ func (s * Section ) addCodeBlock (code , language string ) {
39+ if language != "" {
40+ s .addLine (fmt .Sprintf ("```%s" , language ))
41+ } else {
42+ s .addLine ("```" )
43+ }
44+ s .addLine (code )
45+ s .addLine ("```" )
46+ }
47+
48+ // addBoldText formats and adds bold text.
49+ func (s * Section ) addBoldText (text string ) {
50+ s .addLine (fmt .Sprintf ("**%s**" , text ))
51+ }
52+
53+ // addItalicText formats and adds italic text.
54+ func (s * Section ) addItalicText (text string ) {
55+ s .addLine (fmt .Sprintf ("*%s*" , text ))
56+ }
57+
58+ // addHeader formats and adds a header of specified level (1-6).
59+ func (s * Section ) addHeader (text string , level int ) {
60+ if level < 1 {
61+ level = 1
62+ } else if level > 6 {
63+ level = 6
64+ }
65+ prefix := strings .Repeat ("#" , level )
66+ s .addLine (fmt .Sprintf ("%s %s" , prefix , text ))
67+ }
68+
69+ // addBlockquote formats and adds a blockquote.
70+ func (s * Section ) addBlockquote (text string ) {
71+ // Split multi-line quotes and prefix each line
72+ lines := strings .Split (text , "\n " )
73+ for _ , line := range lines {
74+ s .addLine (fmt .Sprintf ("> %s" , strings .TrimSpace (line )))
75+ }
76+ }
77+
78+ // addLink formats and adds a hyperlink.
79+ func (s * Section ) addLink (text , url string ) {
80+ s .addLine (fmt .Sprintf ("[%s](%s)" , text , url ))
81+ }
82+
83+ // addHorizontalRule adds a horizontal rule.
84+ func (s * Section ) addHorizontalRule () {
85+ s .addLine ("---" )
86+ }
87+
88+ // addTable formats and adds a simple table with headers and rows.
89+ func (s * Section ) addTable (headers []string , rows [][]string ) {
90+ if len (headers ) == 0 {
91+ return
92+ }
93+
94+ // Add header row
95+ headerLine := "| " + strings .Join (headers , " | " ) + " |"
96+ s .addLine (headerLine )
97+
98+ // Add separator row
99+ separators := make ([]string , len (headers ))
100+ for i := range separators {
101+ separators [i ] = "---"
102+ }
103+ separatorLine := "| " + strings .Join (separators , " | " ) + " |"
104+ s .addLine (separatorLine )
105+
106+ // Add data rows
107+ for _ , row := range rows {
108+ if len (row ) > len (headers ) {
109+ row = row [:len (headers )]
110+ } else if len (row ) < len (headers ) {
111+ // Pad with empty cells
112+ for i := len (row ); i < len (headers ); i ++ {
113+ row = append (row , "" )
114+ }
115+ }
116+ rowLine := "| " + strings .Join (row , " | " ) + " |"
117+ s .addLine (rowLine )
118+ }
119+ }
120+
31121// findSubSection recursively searches for a subsection by its name within the current section.
32122// If a matching subsection is found, it is returned; otherwise, nil is returned.
33123func (s * Section ) findSubSection (name string ) * Section {
@@ -161,6 +251,116 @@ func (pb *PromptBuilder) AddListItemF(sectionName string, value interface{}) *Pr
161251 return pb .AddListItem (sectionName , text )
162252}
163253
254+ // AddBulletItem adds a bullet point item to the specified section or subsection.
255+ func (pb * PromptBuilder ) AddBulletItem (sectionName , item string ) * PromptBuilder {
256+ if section := pb .findSection (sectionName ); section != nil {
257+ section .addBulletItem (strings .TrimSpace (item ))
258+ }
259+ return pb
260+ }
261+
262+ // AddBulletItemF is a helper method that converts any value to its string representation
263+ // and adds it as a bullet point item to the specified section.
264+ func (pb * PromptBuilder ) AddBulletItemF (sectionName string , value interface {}) * PromptBuilder {
265+ var text string
266+
267+ if str , ok := value .(string ); ok {
268+ text = str
269+ } else {
270+ bytes , err := json .Marshal (value )
271+ if err != nil {
272+ text = fmt .Sprintf ("%v" , value )
273+ } else {
274+ text = string (bytes )
275+ }
276+ }
277+
278+ return pb .AddBulletItem (sectionName , text )
279+ }
280+
281+ // AddCodeBlock adds a code block with optional language specification to the specified section.
282+ func (pb * PromptBuilder ) AddCodeBlock (sectionName , code , language string ) * PromptBuilder {
283+ if section := pb .findSection (sectionName ); section != nil {
284+ section .addCodeBlock (code , language )
285+ }
286+ return pb
287+ }
288+
289+ // AddCodeBlockF is a helper method that converts any value to its string representation
290+ // and adds it as a code block to the specified section.
291+ func (pb * PromptBuilder ) AddCodeBlockF (sectionName string , value interface {}, language string ) * PromptBuilder {
292+ var text string
293+
294+ if str , ok := value .(string ); ok {
295+ text = str
296+ } else {
297+ bytes , err := json .Marshal (value )
298+ if err != nil {
299+ text = fmt .Sprintf ("%v" , value )
300+ } else {
301+ text = string (bytes )
302+ }
303+ }
304+
305+ return pb .AddCodeBlock (sectionName , text , language )
306+ }
307+
308+ // AddBoldText adds bold text to the specified section.
309+ func (pb * PromptBuilder ) AddBoldText (sectionName , text string ) * PromptBuilder {
310+ if section := pb .findSection (sectionName ); section != nil {
311+ section .addBoldText (strings .TrimSpace (text ))
312+ }
313+ return pb
314+ }
315+
316+ // AddItalicText adds italic text to the specified section.
317+ func (pb * PromptBuilder ) AddItalicText (sectionName , text string ) * PromptBuilder {
318+ if section := pb .findSection (sectionName ); section != nil {
319+ section .addItalicText (strings .TrimSpace (text ))
320+ }
321+ return pb
322+ }
323+
324+ // AddHeader adds a header of specified level to the section.
325+ func (pb * PromptBuilder ) AddHeader (sectionName , text string , level int ) * PromptBuilder {
326+ if section := pb .findSection (sectionName ); section != nil {
327+ section .addHeader (strings .TrimSpace (text ), level )
328+ }
329+ return pb
330+ }
331+
332+ // AddBlockquote adds a blockquote to the specified section.
333+ func (pb * PromptBuilder ) AddBlockquote (sectionName , text string ) * PromptBuilder {
334+ if section := pb .findSection (sectionName ); section != nil {
335+ section .addBlockquote (strings .TrimSpace (text ))
336+ }
337+ return pb
338+ }
339+
340+ // AddLink adds a hyperlink to the specified section.
341+ func (pb * PromptBuilder ) AddLink (sectionName , text , url string ) * PromptBuilder {
342+ if section := pb .findSection (sectionName ); section != nil {
343+ section .addLink (strings .TrimSpace (text ), url )
344+ }
345+ return pb
346+ }
347+
348+ // AddHorizontalRule adds a horizontal rule to the specified section.
349+ func (pb * PromptBuilder ) AddHorizontalRule (sectionName string ) * PromptBuilder {
350+ if section := pb .findSection (sectionName ); section != nil {
351+ section .addHorizontalRule ()
352+ }
353+ return pb
354+ }
355+
356+ // AddTable adds a table with headers and rows to the specified section.
357+ func (pb * PromptBuilder ) AddTable (sectionName string , headers []string , rows [][]string ) * PromptBuilder {
358+ if section := pb .findSection (sectionName ); section != nil {
359+ section .addTable (headers , rows )
360+ }
361+ return pb
362+ }
363+
164364// buildSection recursively generates the string representation of a Section and its nested subsections.
165365// The indent parameter is used to properly format nested content.
166366func buildSection (sec * Section , indent string ) string {
@@ -172,7 +372,7 @@ func buildSection(sec *Section, indent string) string {
172372 // Append each line of the section, ensuring proper trimming and formatting.
173373 for _ , line := range sec .Lines {
174374 sb .WriteString (indent )
175- sb .WriteString (strings . TrimSpace ( line ) )
375+ sb .WriteString (line )
176376 sb .WriteString ("\n " )
177377 }
178378
0 commit comments