Class: Utopia::Project::Linkify

Inherits:
Decode::Syntax::Rewriter
  • Object
show all
Defined in:
lib/utopia/project/linkify.rb

Overview

Rewrites source code as HTML with links to indexed definitions.

Instance Method Summary collapse

Constructor Details

#initialize(base, language, text) ⇒ Linkify

Initialize a source code rewriter.



16
17
18
19
20
21
# File 'lib/utopia/project/linkify.rb', line 16

def initialize(base, language, text)
	@base = base
	@language = language
	
	super(text)
end

Instance Method Details

#apply(output = XRB::Builder.new) ⇒ Object

Apply the source links and wrap the result in a code element.



47
48
49
50
51
52
53
# File 'lib/utopia/project/linkify.rb', line 47

def apply(output = XRB::Builder.new)
	output.inline("code", class: "language-#{@language.name}") do
		super
	end
	
	return output.to_str
end

Build a link to an indexed definition.



36
37
38
39
40
41
42
# File 'lib/utopia/project/linkify.rb', line 36

def link_to(definition, text)
	XRB::Builder.fragment do |builder|
		builder.inline("a", href: @base.link_for(definition), title: definition.qualified_name) do
			builder.text(text)
		end
	end
end

#text_for(range) ⇒ Object

Escape an unmatched range of source text for HTML output.



26
27
28
29
30
# File 'lib/utopia/project/linkify.rb', line 26

def text_for(range)
	text = super(range)
	
	return XRB::Strings.to_html(text)
end