<commit_before>package com.aljoschability.eclipse.tools.edogen.impl

import com.aljoschability.eclipse.tools.edogen.AbstractGenerator
import java.util.Collection
import java.util.Map
import org.eclipse.emf.ecore.EAttribute
import org.eclipse.emf.ecore.EClass
import org.eclipse.emf.ecore.EClassifier
import org.eclipse.emf.ecore.EDataType
import org.eclipse.emf.ecore.EEnum
import org.eclipse.emf.ecore.EModelElement
import org.eclipse.emf.ecore.ENamedElement
import org.eclipse.emf.ecore.EOperation
import org.eclipse.emf.ecore.EPackage
import org.eclipse.emf.ecore.EReference
import org.eclipse.emf.ecore.ETypedElement
import org.eclipse.emf.ecore.util.EcoreUtil
import org.eclipse.xtend2.lib.StringConcatenation

/*
 * TODO:
 * <ul>
 * <li>tex escape: '{', '}', '\', '$'
 * <li>html escape: <a>,<strong>,<em>,<code>,<pre>,<p>
 * </ul>
 */
class LatexDocumentationGenerator extends AbstractGenerator {
	val TEXT_EXTENDS = "extends"

	def title(Object e) {
		switch e {
			EPackage case e.ESuperPackage == null: '''Package \texttt{«e.escaped_name»}'''
			EPackage: {
				var StringBuilder b = new StringBuilder

				var s = e
				while (s != null) {
					b.insert(0, s.escaped_name)

					s = s.ESuperPackage

					if (s != null) {
						b.insert(0, "::")
					}
				}

				'''Package \texttt{«b»}'''
			}
			EClass case e.interface: '''Interface \texttt{«e.escaped_name»}'''
			EClass case e.abstract: '''Abstract Class \texttt{«e.escaped_name»}'''
			EClass: '''Class \texttt{«e.escaped_name»}'''
			EEnum: '''Enumeration \texttt{«e.escaped_name»}'''
			EDataType: '''Data Type \texttt{«e.escaped_name»}'''
		}
	}

	var Map<EClass, Collection<EClass>> subclassCache

	def reference(EModelElement element) {
		var uri = EcoreUtil.getURI(element)
		"api:" + (uri.lastSegment + uri.fragment).replace("/", ":")
	}

	override generate(EPackage... elements) {

		// create subclass cache
		subclassCache = newLinkedHashMap()

		// go through packages
		for (pack : elements) {

			// go through classifiers
			for (classifier : pack.EClassifiers) {
				switch classifier {
					// for all classes
					EClass:
						// go through super classes
						for (superclass : classifier.ESuperTypes) {
							var subclasses = subclassCache.get(superclass)
							if (subclasses == null) {
								subclasses = newLinkedHashSet()
								subclassCache.put(superclass, subclasses)
							}
							subclasses.add(classifier)
						}
				}
			}
		}

		// generate it
		var StringConcatenation result = new StringConcatenation;
		for (pack : elements) {
			if (!pack.hidden) {
				result.append(generate(pack))
			}
		}
		result.toString
	}

	def children(EClass element) {
		subclassCache.get(element)
	}

	def parents(EClass element) {
		if (element.ESuperTypes.empty) {
			null
		} else {
			element.ESuperTypes
		}
	}

	def generate(EPackage pack) '''
		\section{«pack.title»}
		\label{«pack.reference»}
		«pack.documentation»
		\begin{description}
		\item[Namespace] \texttt{«pack.nsURI»}
		«IF !pack.name.equals(pack.nsPrefix)»
			\item[Prefix] \texttt{«pack.nsPrefix»}
		«ENDIF»
		\end{description}
		«FOR c : pack.EClassifiers»
			«IF !c.hidden»
				«create(c)»
			«ENDIF»
		«ENDFOR»
	'''

	def create(EClassifier c) {
		switch c {
			EClass: create(c)
			EEnum: create(c)
			EDataType: create(c)
			default: throw new UnsupportedOperationException()
		}
	}
<commit_msg>small changes to latex generator<commit_after>package com.aljoschability.eclipse.tools.edogen.impl

import com.aljoschability.eclipse.tools.edogen.AbstractGenerator
import java.util.Collection
import java.util.Map
import org.eclipse.emf.ecore.EAttribute
import org.eclipse.emf.ecore.EClass
import org.eclipse.emf.ecore.EClassifier
import org.eclipse.emf.ecore.EDataType
import org.eclipse.emf.ecore.EEnum
import org.eclipse.emf.ecore.EModelElement
import org.eclipse.emf.ecore.ENamedElement
import org.eclipse.emf.ecore.EOperation
import org.eclipse.emf.ecore.EPackage
import org.eclipse.emf.ecore.EReference
import org.eclipse.emf.ecore.ETypedElement
import org.eclipse.emf.ecore.util.EcoreUtil
import org.eclipse.xtend2.lib.StringConcatenation

/*
 * TODO:
 * <ul>
 * <li>tex escape: '{', '}', '\', '$'
 * <li>html escape: <a>,<strong>,<em>,<code>,<pre>,<p>
 * </ul>
 */
class LatexDocumentationGenerator extends AbstractGenerator {
	val Collection<String> labels = newLinkedHashSet

	def static private String getLabel(EModelElement element) {
		val prefix = "api"
		val uri = EcoreUtil::getURI(element)

		return prefix + uri

	//"api:" + (uri.lastSegment + uri.fragment).replace("/", ":")
	}

	val TEXT_EXTENDS = "extends"

	def title(Object e) {
		if (e instanceof EModelElement) {
			println(e.label)
		}

		switch e {
			EPackage case e.ESuperPackage == null: '''Package \texttt{«e.escaped_name»}'''
			EPackage: {
				var StringBuilder b = new StringBuilder

				var s = e
				while (s != null) {
					b.insert(0, s.escaped_name)

					s = s.ESuperPackage

					if (s != null) {
						b.insert(0, "::")
					}
				}

				'''Package \texttt{«b»}'''
			}
			EClass case e.interface: '''Interface \texttt{«e.escaped_name»}'''
			EClass case e.abstract: '''Abstract Class \texttt{«e.escaped_name»}'''
			EClass: '''Class \texttt{«e.escaped_name»}'''
			EEnum: '''Enumeration \texttt{«e.escaped_name»}'''
			EDataType: '''Data Type \texttt{«e.escaped_name»}'''
		}
	}

	var Map<EClass, Collection<EClass>> subclassCache

	def reference(EModelElement element) {
		var uri = EcoreUtil.getURI(element)
		"api:" + (uri.lastSegment + uri.fragment).replace("/", ":")
	}

	override generate(EPackage... elements) {

		// create subclass cache
		subclassCache = newLinkedHashMap()

		// go through packages
		for (pack : elements) {

			// go through classifiers
			for (classifier : pack.EClassifiers) {
				switch classifier {
					// for all classes
					EClass:
						// go through super classes
						for (superclass : classifier.ESuperTypes) {
							var subclasses = subclassCache.get(superclass)
							if (subclasses == null) {
								subclasses = newLinkedHashSet()
								subclassCache.put(superclass, subclasses)
							}
							subclasses.add(classifier)
						}
				}
			}
		}

		// generate it
		var StringConcatenation result = new StringConcatenation
		for (pack : elements) {
			if (!pack.hidden) {
				result.append(generate(pack))
			}
		}
		result.toString
	}

	def children(EClass element) {
		subclassCache.get(element)
	}

	def parents(EClass element) {
		if (element.ESuperTypes.empty) {
			null
		} else {
			element.ESuperTypes
		}
	}

	def generate(EPackage pack) {
		labels += pack.reference
		return '''
			\section{«pack.title»}
			\label{«pack.reference»}
			«pack.documentation»
			\begin{description}
			\item[Namespace] \texttt{«pack.nsURI»}
			«IF !pack.name.equals(pack.nsPrefix)»
				\item[Prefix] \texttt{«pack.nsPrefix»}
			«ENDIF»
			\end{description}
			«FOR c : pack.EClassifiers»
				«IF !c.hidden»
					«create(c)»
				«ENDIF»
			«ENDFOR»
		'''
	}

	def create(EClassifier c) {
		switch c {
			EClass: create(c)
			EEnum: create(c)
			EDataType: create(c)
			default: throw new UnsupportedOperationException()
		}
	}

{'fraction_non_alphanumeric': 0.14237573715248525, 'fraction_numerical': 0.0008424599831508003, 'mean_word_length': 3.471437539234149, 'pattern_counts': {'":': 5, '<': 28, '<?xml version=': 0, '>': 28, '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': '5942291', 'n_tokens_mistral': 2923, 'n_tokens_neox': 2472, 'n_words': 580}