Sublime Forum

Crazy-dynamic highlighting for hyperlinking

#1

Hi, everyone. Got a bit of a conundrum and wondered if anyone had a clue about how I might implement something. The quick version is this; can you dynamically generate a tmLanguage file and then assign it to a view? That vis, something that looks like this;

class automaticGrammarsPlugin(sublimePlugin.Plugin):
	def onLoad(self, view):
		grammar = MakeNewLanguageGrammar(view.fileName())
		AssignGrammarToView(view)

Now, that’s what I want to do. Here’s why.

I’m putting together a plugin for hyperlinking between files. If a directory contains

  \mydirectory
    \foo.txt
    \bar.txt
    \baz.txt

then there is a special grammar for this folder, highlighting the words “foo”, “bar”, and “baz” in any of these files;

Should make for a very useful little file-based hyperlinking/wiki system. But there’s a crazy sort of dynamism here; each directory needs it’s own language. Go into a different folder and you get an entirely different set of highlighted terms.

So I figure I’m going to have to automatically generate the grammar file (not too hard), then assign this custom grammar to the view (the hard bit.)

Anyone got any ideas?

Also, anyone got any requirements for a wiki-like system? I’ll see if I can bear them in mind, but I can’t promise anything.

0 Likes

#2

should you want to try it out, there’s a first version available at sublimetextwiki.com/sublime- … me-package

README.txt ==

Hyperlinking Sublime Package, by Steve Cooper

This provides a simple hyperlinking facility.

You use it like this; create a folder and add at least one file with a “.wiki” extension. Open a file up, and insert hyperlinks like this;

visit the [Other File]

Place your cursor inside the “[Other File]” text and hit ctrl-alt-n (navigate). You’ll be prompted to create the file, then it’ll be opened up. You can use this to create a rudimentary wiki.

0 Likes

#3

Hi, Nick.

Why do you need the dynamic highlighting? If you are using square brackets etc and the links are fairly distinct a static hard coded pattern would be ok yeah? Or are the square brackets a workaround for the moment? …] A markup converted to html not cutting it for you huh?

My intended audience are very non-technical. I’m putting together some tools for authors, who I think could benefit from some of the great goo available in text editors that isn’t available from Word. For example, authors need version control as much as developers, but they don’t really know about it; Sublime Text with Automatic Backups seems like a great starter in this area.

What comes next is richer manuscripts; if I have this paragraph;

Then I want the user to be able to create a suplementary file, Jake.wiki, by putting the cursor inside the string “Jake” and pressing a key. That means that the manuscript gets linked in to all the author’s other notes, which is a big win. Thing is, I’m trying to keep the markup so light that authors don’t realise they’re using markup. If my instructions read ‘there is a special code for…,’ then I’ve failed.

The square brackets are more of an advanced feature, for longer phrases;

Although, if I’m really slick, I’ll be able to detect the presence of a “Flash Gordon.wiki” file, and allow the user to navigate to it without using the square brackets.

So yeah, the square brackets are a pragmatic choice so that I can get something working ASAP.

Are you going to have facility for linking between different directories?

Already there if you use square brackets;

[code]why not travel to ..\my_other_wiki\other page][/code]

Otherwise, I don’t know yet how I’m going to do this.

0 Likes

#4

Check out a piece of software called “The Brain”

I’ve seen the brain – it’s really pretty, and I like it in principle, but always find myself gravitating back towards text. Hence this little project.

The idea sounds like it would be really handy. Useful for navigating between files as well if you make the engine generic. eg opening css/ js files linked to from within a html file.

Now that’s a very nice idea. I could possibly pair regexen to opening-functions; eg

"import ([a-z])" => OpenPythonImportFile("$1");
"href="(.*)" => OpenHtmlFile("$1");
"[A-Z][a-z]{,2}" => OpenWikiWord("$1");

I’ll have a think…

0 Likes

#5

Steve - assigning a syntax definition to a buffer on the fly is fairly straight forward, you just need something along the lines of:

view.options.set('syntax', u'Packages/Foo/Foo.tmLanguage')
0 Likes

#6

Great, jon. That may be all I need.

0 Likes