<commit_before>package pigae

import (
	"net/http"

	. "github.com/Deleplace/programming-idioms/pig"
	"github.com/gorilla/mux"
	"google.golang.org/appengine"
	gaesearch "google.golang.org/appengine/search"
)

//
// A Cheat Sheet is a single page containing the implementations of all idioms, for a
// single language.
//

// AllIdiomsFacade is the Facade for the Cheat Sheets.
type CheatSheetFacade struct {
	PageMeta        PageMeta
	UserProfile     UserProfile
	Lang            string
	CheatsheetLines []cheatSheetLineDoc
}

func cheatsheet(w http.ResponseWriter, r *http.Request) error {
	vars := mux.Vars(r)
	lang := vars["lang"]
	c := appengine.NewContext(r)

	// Security belt. Might be changed if needed.
	limit := 1000

	// This uses the Search API to retrieve just the data we need.
	cheatsheetLines, err := dao.getCheatSheet(c, lang, limit)
	if err != nil {
		return PiError{err.Error(), http.StatusInternalServerError}
	}

	data := CheatSheetFacade{
		PageMeta: PageMeta{
			PageTitle: printNiceLang(lang) + " cheat sheet",
			Toggles:   toggles,
		},
		UserProfile:     readUserProfile(r),
		Lang:            lang,
		CheatsheetLines: cheatsheetLines,
	}

	if err := templates.ExecuteTemplate(w, "page-cheatsheet", data); err != nil {
		return PiError{err.Error(), http.StatusInternalServerError}
	}
	return nil
}

// useful for calling markup2CSS on cheatSheetLineDoc fields
func atom2string(atom gaesearch.Atom) string {
	return string(atom)
}<commit_msg>Cheatsheet handle multiple impls without stuttering.<commit_after>package pigae

import (
	"net/http"

	. "github.com/Deleplace/programming-idioms/pig"
	"github.com/gorilla/mux"
	"google.golang.org/appengine"
	gaesearch "google.golang.org/appengine/search"
)

//
// A Cheat Sheet is a single page containing the implementations of all idioms, for a
// single language.
//

// AllIdiomsFacade is the Facade for the Cheat Sheets.
type CheatSheetFacade struct {
	PageMeta        PageMeta
	UserProfile     UserProfile
	Lang            string
	CheatsheetLines []cheatSheetLineDoc
}

func cheatsheet(w http.ResponseWriter, r *http.Request) error {
	vars := mux.Vars(r)
	lang := vars["lang"]
	c := appengine.NewContext(r)

	// Security belt. Might be changed if needed.
	limit := 1000

	// This uses the Search API to retrieve just the data we need.
	cheatsheetLines, err := dao.getCheatSheet(c, lang, limit)
	if err != nil {
		return PiError{err.Error(), http.StatusInternalServerError}
	}

	// Don't repeat idiom ID, title, lead on consecutive rows.
	// Note: this *may* not play well with the JS text filter.
	for i := 1; i < len(cheatsheetLines); i++ {
		if cheatsheetLines[i].IdiomID == cheatsheetLines[i-1].IdiomID {
			cheatsheetLines[i].IdiomID = ""
			cheatsheetLines[i].IdiomTitle = ""
			cheatsheetLines[i].IdiomLeadParagraph = "Alternative implementation"
		}
	}

	data := CheatSheetFacade{
		PageMeta: PageMeta{
			PageTitle: printNiceLang(lang) + " cheat sheet",
			Toggles:   toggles,
		},
		UserProfile:     readUserProfile(r),
		Lang:            lang,
		CheatsheetLines: cheatsheetLines,
	}

	if err := templates.ExecuteTemplate(w, "page-cheatsheet", data); err != nil {
		return PiError{err.Error(), http.StatusInternalServerError}
	}
	return nil
}

// useful for calling markup2CSS on cheatSheetLineDoc fields
func atom2string(atom gaesearch.Atom) string {
	return string(atom)
}
{'fraction_non_alphanumeric': 0.10925870794879429, 'fraction_numerical': 0.004167907115212861, 'mean_word_length': 3.8206599713055955, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2820750', 'n_tokens_mistral': 1186, 'n_tokens_neox': 1111, 'n_words': 299}