...

Source file src/github.com/noirbizarre/gonja/utils/lorem.go

Documentation: github.com/noirbizarre/gonja/utils

     1  package utils
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"strings"
     7  
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  const LOREM_IPSUM = `Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    12  Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
    13  Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
    14  Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
    15  Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis.
    16  At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, At accusam aliquyam diam diam dolore dolores duo eirmod eos erat, et nonumy sed tempor et et invidunt justo labore Stet clita ea et gubergren, kasd magna no rebum. sanctus sea sed takimata ut vero voluptua. est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.
    17  Consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.`
    18  
    19  const LOREM_IPSUM_WORDS = `a ac accumsan ad adipiscing aenean aliquam aliquet amet ante aptent arcu at
    20  auctor augue bibendum blandit class commodo condimentum congue consectetuer
    21  consequat conubia convallis cras cubilia cum curabitur curae cursus dapibus
    22  diam dictum dictumst dignissim dis dolor donec dui duis egestas eget eleifend
    23  elementum elit enim erat eros est et etiam eu euismod facilisi facilisis fames
    24  faucibus felis fermentum feugiat fringilla fusce gravida habitant habitasse hac
    25  hendrerit hymenaeos iaculis id imperdiet in inceptos integer interdum ipsum
    26  justo lacinia lacus laoreet lectus leo libero ligula litora lobortis lorem
    27  luctus maecenas magna magnis malesuada massa mattis mauris metus mi molestie
    28  mollis montes morbi mus nam nascetur natoque nec neque netus nibh nisi nisl non
    29  nonummy nostra nulla nullam nunc odio orci ornare parturient pede pellentesque
    30  penatibus per pharetra phasellus placerat platea porta porttitor posuere
    31  potenti praesent pretium primis proin pulvinar purus quam quis quisque rhoncus
    32  ridiculus risus rutrum sagittis sapien scelerisque sed sem semper senectus sit
    33  sociis sociosqu sodales sollicitudin suscipit suspendisse taciti tellus tempor
    34  tempus tincidunt torquent tortor tristique turpis ullamcorper ultrices
    35  ultricies urna ut varius vehicula vel velit venenatis vestibulum vitae vivamus
    36  viverra volutpat vulputate`
    37  
    38  var (
    39  	LoremParagraphs = strings.Split(LOREM_IPSUM, "\n")
    40  	LoremWords      = strings.Fields(LOREM_IPSUM)
    41  	WORDS           = strings.Fields(LOREM_IPSUM_WORDS)
    42  )
    43  
    44  func Lorem(count int, method string) (string, error) {
    45  	var out strings.Builder
    46  	switch method {
    47  	case "b":
    48  		for i := 0; i < count; i++ {
    49  			if i > 0 {
    50  				out.WriteString("\n")
    51  			}
    52  			par := LoremParagraphs[i%len(LoremParagraphs)]
    53  			out.WriteString(par)
    54  		}
    55  	case "w":
    56  		for i := 0; i < count; i++ {
    57  			if i > 0 {
    58  				out.WriteString(" ")
    59  			}
    60  			word := LoremWords[i%len(LoremWords)]
    61  			out.WriteString(word)
    62  		}
    63  	case "p":
    64  		for i := 0; i < count; i++ {
    65  			if i > 0 {
    66  				out.WriteString("\n")
    67  			}
    68  			out.WriteString("<p>")
    69  			par := LoremParagraphs[i%len(LoremParagraphs)]
    70  			out.WriteString(par)
    71  			out.WriteString("</p>")
    72  
    73  		}
    74  	default:
    75  		return "", errors.Errorf("unsupported method: %s", method)
    76  	}
    77  
    78  	return out.String(), nil
    79  }
    80  
    81  func Lipsum(n int, html bool, min int, max int) string {
    82  	result := []string{}
    83  
    84  	for i := 0; i < n; i++ {
    85  		nextCapitalized := true
    86  		lastComma, lastFullstop := 0, 0
    87  		word := ""
    88  		last := ""
    89  		p := []string{}
    90  
    91  		// each paragraph contains out of min to max words.
    92  		for j := min; j < max; j++ {
    93  			for {
    94  				word = WORDS[rand.Intn(len(WORDS))]
    95  				if word != last {
    96  					last = word
    97  					break
    98  				}
    99  			}
   100  
   101  			if nextCapitalized {
   102  				word = strings.Title(word)
   103  				nextCapitalized = false
   104  			}
   105  
   106  			if j-(3+rand.Intn(5)) > lastComma {
   107  				// Add comas
   108  				lastComma = j
   109  				lastFullstop += 2
   110  				word += ","
   111  			} else if j-(10+rand.Intn(10)) > lastFullstop {
   112  				// Add end of sentences
   113  				lastComma, lastFullstop = j, j
   114  				word += "."
   115  				nextCapitalized = true
   116  			}
   117  
   118  			p = append(p, word)
   119  		}
   120  
   121  		// # ensure that the paragraph ends with a dot.
   122  		str := strings.Join(p, " ")
   123  		if strings.HasSuffix(str, ",") {
   124  			str = str[:len(str)-1] + "."
   125  		} else if !strings.HasSuffix(str, ".") {
   126  			str += "."
   127  		}
   128  
   129  		result = append(result, str)
   130  	}
   131  
   132  	if !html {
   133  		return strings.Join(result, "\n\n")
   134  	}
   135  	htmlResult := []string{}
   136  	for _, p := range result {
   137  		htmlResult = append(htmlResult, fmt.Sprintf(`<p>%s<p>`, p))
   138  	}
   139  	return strings.Join(htmlResult, "\n")
   140  }
   141  
   142  // Generates some lorem ipsum for the template.
   143  // By default, five paragraphs of HTML are generated
   144  // with each paragraph between 20 and 100 words.
   145  // If html is False, regular text is returned.
   146  // func lipsum(n=5, html=True, min=20, max=100) {}
   147  // func Lorem(count int, method string) (string, error) {
   148  // 	var out strings.Builder
   149  // 	switch method {
   150  // 	case "b":
   151  // 		if random {
   152  // 			for i := 0; i < count; i++ {
   153  // 				if i > 0 {
   154  // 					out.WriteString("\n")
   155  // 				}
   156  // 				par := LoremParagraphs[rand.Intn(len(LoremParagraphs))]
   157  // 				out.WriteString(par)
   158  // 			}
   159  // 		} else {
   160  // 			for i := 0; i < count; i++ {
   161  // 				if i > 0 {
   162  // 					out.WriteString("\n")
   163  // 				}
   164  // 				par := LoremParagraphs[i%len(LoremParagraphs)]
   165  // 				out.WriteString(par)
   166  // 			}
   167  // 		}
   168  // 	case "w":
   169  // 		if random {
   170  // 			for i := 0; i < count; i++ {
   171  // 				if i > 0 {
   172  // 					out.WriteString(" ")
   173  // 				}
   174  // 				word := LoremWords[rand.Intn(len(LoremWords))]
   175  // 				out.WriteString(word)
   176  // 			}
   177  // 		} else {
   178  // 			for i := 0; i < count; i++ {
   179  // 				if i > 0 {
   180  // 					out.WriteString(" ")
   181  // 				}
   182  // 				word := LoremWords[i%len(LoremWords)]
   183  // 				out.WriteString(word)
   184  // 			}
   185  // 		}
   186  // 	case "p":
   187  // 		if random {
   188  // 			for i := 0; i < count; i++ {
   189  // 				if i > 0 {
   190  // 					out.WriteString("\n")
   191  // 				}
   192  // 				out.WriteString("<p>")
   193  // 				par := LoremParagraphs[rand.Intn(len(LoremParagraphs))]
   194  // 				out.WriteString(par)
   195  // 				out.WriteString("</p>")
   196  // 			}
   197  // 		} else {
   198  // 			for i := 0; i < count; i++ {
   199  // 				if i > 0 {
   200  // 					out.WriteString("\n")
   201  // 				}
   202  // 				out.WriteString("<p>")
   203  // 				par := LoremParagraphs[i%len(LoremParagraphs)]
   204  // 				out.WriteString(par)
   205  // 				out.WriteString("</p>")
   206  
   207  // 			}
   208  // 		}
   209  // 	default:
   210  // 		return "", errors.Errorf("unsupported method: %s", method)
   211  // 	}
   212  
   213  // 	return out.String(), nil
   214  // }
   215  

View as plain text