Git Product home page Git Product logo

lotgdformat's Introduction

LotgdFormat

A C# implementation of the LotgD style formatting code for text formatting.

How to use this library

See API documentation. (TODO)

Acknowledgements

This library is based on the appoencode() function found in LotgD 0.9.7. DragonSlayer V2.5. Known contributors to that specific version of the function are:

  • MightyE
  • JT Traub
  • Eliwood
  • Talion

Known issues

The original handles tags followed by "`0" differently than this library. I am not quite sure if the original has a bug there or if the behavior should be replicated here.

Example - original:

input:
`$red`Hnavhi`0 still red`0 still red.

output (indented for easier reading):
<span class="c36">
	red
	<span class='navhi'>
		navhi
	</span>
	still red
	still red.

Same input in this libary:

input:
`$red`Hnavhi`0 still red`0 still red.

output (indented for easier reading):
<span class=\"c36\">
	red
	<span class=\"navhi\">
		navhi
	</span>
</span>
<span class=\"navhi\">
	still red still red.

How to use

First, instantiate a formatter, then use AddText()

var formatter = new Formatter([
	new LotgdFormatCode('n', tag: "br", selfClosing: true),
	new LotgdFormatCode('b', tag: "b"),
	new LotgdFormatCode('i', tag: "i"),
	new LotgdFormatCode('@', color: "00FF00"),
	new LotgdFormatCode('$', color: "00FF00"),
]);

var html = formatter.AddText("`bLegend`b of the `@Green`0 Dragon.");
// html = "<b>Legend<b> of the <span class='c64'>Green</span> Dragon"

The formatter does keep track of tags that were opened but not closed.

You can check the status via Formatter.IsClear().

And generate the appropriate closing tags with Formatter.CloseOpenTags().

Code syntax

Every code is marked with a backtick ` and consists of a single arbitrary character.

Example:

This text is `@green from here.

There are 4 types of codes:

  • Color codes.
  • Single format codes
  • Paired format codes
  • The closing code.

Color codes

A color code creates a span tag with a specific class to change the text color within. The tag will encapsulate all text until either:

  • The end of the text.
  • The next color is encountered
  • The closing code is encountered.

Examples:

`@this is green `$this is red.
=>
<span class="c64">this is green</span>
<span class="c36">this is red</span>

This library treats all codes that only have a token and a color as color codes.

Self-closing format codes

A self-closing format codes is used to generate self closing tags in the output. The most prominent example in LotgD is n for a <br>. Another possible usecase would be for creating an hr tag.

Since they are self contained, they have no interactions with other codes other than possibily being embedded.

Example:

This is line one.`nThis is line two.
=>
This is line one.<br/>This is line two.

This library only treats codes configured as such to be self closing.

Regular format codes

These create a formatting tag such as strong, i, or center. They enclose the text following the tag until either the end of the text or if another instance of the same tag is encountered.

The closing tag does not affect them.

Example:

`bThis is bold `ithis is italic
=>
<strong>This is bold <i>this is bold and italic<i></strong>

They can however be closed with themselves:

`bThis is bold`b `ithis is bold and italic`i. This is normal.
=>
<strong>This is bold</strong> <i>this is italic<i>. This is normal.

This library treats all codes as regular format codes if they have a tag specified, even if also a color is specified.

The closing code

The code 0 closes the currently open color span.

`bBold `iand italic `$and red`0. This is no longer red.
=>
<strong>
	Bold
	<i>
		and italic
		<span class="lotgd-c36">and red</span>
	</i>
</strong>
<strong>
	<i>
		. This is no longer red.
	</i>
</strong>

This code is reserved and not configurable.

Escaping

To render the backtick (`), the backtick needs to be doubled up:

This is a backtick: ``
=>
This is a backtick: `

Appendix

The code definitions used in this files examples are:

Token Color HTML-Tag Self-closing
n - <br> true
b - <strong> false
i - <i> false
@ 00FF00 none false
$ FF0000 none false
0 - - -

lotgdformat's People

Contributors

stringepsilon avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.