Source

wikdNode / shared / join.groovy

The branch 'default' does not exist.
Full commit
import org.apache.commons.lang.StringUtils as SU
import org.freeplane.core.util.HtmlUtils as HU

def joinPlain(String mark) {
	node.text = SU.join(
			c.selecteds.collect { SU.trim(it.plainText) }.toArray(), mark
			)
	c.selecteds.tail().each { it.delete() }
}

def joinAll(String mark) {
	// core text
	def core, coreMarkup
	def cores = []
	def coreHtml = false
	def coreHtmlNode = false
	// note text
	def note, noteMarkup
	def notes = []
	def noteHtml = false
	def noteHtmlNode = false
	// details text
	def detail, detailMarkup
	def details = []
	def detailHtml = false
	def detailHtmlNode = false
	// all children
	def children = []
	// all icons
	def icons = []

	// classify
	c.selecteds.each { n ->
		// core text
		if (n.text != null ) {
			core = n.text
			coreHtmlNode = HU.isHtmlNode(core)
			cores.add(core)
			coreHtml = coreHtml || coreHtmlNode
		}
		// note text
		if (n.note != null) {
			note = n.noteText
			noteHtmlNode = HU.isHtmlNode(note)
			notes.add(note)
			noteHtml = noteHtml || noteHtmlNode
		}
		if (n.details != null) {
			// detail text
			detail = n.detailsText
			detailHtmlNode = HU.isHtmlNode(detail)
			details.add(detail)
			detailHtml = detailHtml || detailHtmlNode
		}
		// children
		//if (!n.children.empty()) {
		//	children.addAll(n.children)
		//}
		// icons
		if (n.icons != null) {
			icons.addAll(n.icons?.icons)
		}
	}

	def tmpMarkup, writer, body

	// core texts
	if (cores.size() > 0) {
		// core texts with HTML
		if (coreHtml) {
			coreMarkup = new XmlParser().parseText(
					'<html><head></head><body></body></html>'
					)
			head = coreMarkup.head[0]
			body = coreMarkup.body[0]
			cores.each {
				if (HU.isHtmlNode(it)) {
					tmpMarkup = new XmlParser().parseText(it)
					if (tmpMarkup.head) {
						tmpMarkup.head?.children().each { part ->
							head.append(part.clone())
						}
					}
					tmpMarkup.body[0].children().each { part ->
						body.append(part.clone())
						body.appendNode('span', mark)
					}
				} else if (SU.trim(it) != '') {
					body.appendNode('span', it)
					body.appendNode('span', mark)
				}
			}
			writer = new StringWriter()
			new XmlNodePrinter(new PrintWriter(writer)).print(coreMarkup)
			node.text = writer.toString()
			// core texts without
		} else {
			node.text = SU.join(cores.toArray(), mark)
		}
	}

	// notes
	if (notes.size() > 0) {
		// notes with HTML
		if (noteHtml) {
			noteMarkup = new XmlParser().parseText(
					'<html><head></head><body></body></html>'
					)
			body = noteMarkup.body[0]
			ui.informationMessage(body.toString())
			notes.each {
				tmpMarkup = new XmlParser().parseText(it)
				tmpMarkup.body[0].children().each { part -> body.append(part) }
			}
			writer = new StringWriter()
			new XmlNodePrinter(new PrintWriter(writer)).print(noteMarkup)
			node.noteText = writer.toString()
			// notes without
		} else {
			node.noteText = SU.join(notes.toArray(), mark)
		}
	}

	// details
	if (details.size() > 0) {
		// details with HTML
		if (detailHtml) {
			detailMarkup = new XmlParser().parseText('<html><head></head><body></body></html>')
			body = detailMarkup.body[0]
			details.each {
				tmpMarkup = new XmlParser().parseText(it)
				tmpMarkup.body[0].children().each { part -> body.append(part) }
			}
			writer = new StringWriter()
			new XmlNodePrinter(new PrintWriter(writer)).print(detailMarkup)
			node.detailsText = writer.toString()
			// details without
		} else {
			node.detailsText = SU.join(details.toArray(), mark)
		}
	}

	// icons
	if (icons.size() > 0) {
		def ikons = node.icons
		ikons?.icons?.every { ikons?.remove(it) }
		icons.every { ikons?.add(it) }
	}
	// children
	//if (!children.empty()) {
	//	children.each { node.appendChild(it) }
	//}*/

	// cleanup
	c.selecteds.toList().tail().every { it.delete() }

	// present
	c.select(node)
	c.centerOnNode(node)
}

def pick(String mark) {
	if (mark != null) {
		mark = mark ? mark : ''
		if (Boolean.parseBoolean(config.getProperty('wikd_plain_text'))) {
			joinPlain(mark)
		} else {
			joinAll(mark)
		}
	}
}