Skip to content

Commit

Permalink
docs: update README
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovizro committed Nov 30, 2022
1 parent 1847b35 commit 0f8f2e4
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 46 deletions.
82 changes: 55 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,53 @@ From source code:

KoiLang is a markup language while is easy to read for people.
There is an simple example.

#hello KoiLang
I am glad to meet you!

```
#background Street
#camera on(Orga)
#character Orga
Huh... I'm a pretty good shot, huh?
#camera on(Ride, Ched)
#character Ride
B- boss...
#character Orga
#action bleed
#camera on(object: blood, source: Orga)
#camera on(Ched, Orga, Ride)
#character Orga
How come you're stammering like that... Ride!
#playsound freesia
#character Orga
#action stand_up speed(slowly)
#character Ride
But... but!
#character Orga
I'm the Boss of Tekkadan, Orga Itsuka, this is nothing to me.
#character Ride
#action shed_tear
No... not for me...
#camera on(Orga)
#character Orga
Protecting my members is my job!
#character Ched
#action shed_tear
#character Ride
But...!
#character Orga
Shut up and let's go!
#camera on(Orga)
#action walk direction(front) speed(slowly)
Everyone's waiting, besides...
I finally understand now, Mika, we don't need any destinations, we just need to keep moving forward.
As long as we don't stop, the road will continue!
```

In KoiLang, file is divided into 'command' part and 'text' part.
The formation of command part is like C preprocessor directive,
Expand Down Expand Up @@ -99,27 +142,10 @@ Then, we make a script to explain how to do with these commands:

```py
import os
from typing import Union
from kola import KoiLang, BaseLexer, kola_command, kola_text
from kola import KoiLang, kola_command, kola_text, kola_env


class MultiFileManager(KoiLang):
def __init__(self) -> None:
super().__init__()
self._file = None

@kola_command
def space(self, name: str) -> None:
path = name.replace('.', '/')
if not os.path.isdir(path):
os.makedirs(path)
os.chdir(path)

@kola_command
def endspace(self) -> None:
os.chdir("..")
self.end()

@kola_command
def file(self, path: str, encoding: str = "utf-8") -> None:
if self._file:
Expand All @@ -141,8 +167,10 @@ class MultiFileManager(KoiLang):
raise OSError("write texts before the file open")
self._file.write(text)

def parse(self, lexer: Union[BaseLexer, str]) -> None:
super().parse(lexer)
def at_start(self) -> None:
self._file = None

def at_end(self) -> None:
self.end()
```

Expand Down Expand Up @@ -176,6 +204,8 @@ It seems amusing? Well, if you make a python script as this:

```py
vmobj = MultiFileManager()

vmobj.at_start() # begin parsing
vmobj.file("hello.txt", encoding="utf-8")
vmobj.text("Hello world!")
vmobj.text("And there are all my friends.")
Expand All @@ -189,7 +219,7 @@ vmobj.file("Alice.txt")
vmobj.text("Hello Alice.")

vmobj.endspace()
vmobj.end() # from parse
vmobj.at_end() # end parsing
```
the same result will be get. This is the python script corresponding to the previous kola file. What we have done is to make KoiLang interpreter know the correspondence between kola commands and python functions.

Expand Down Expand Up @@ -238,8 +268,6 @@ def endspace(self) -> None:
...
```

> Notice: `@kola_env` has the similar arguments to the `@kola_command`, but the first string argument in `@kola_env` is the **environment name**, not **command name** in kola files. Use `@kola_env(cmd_name="new_name")` to define the command name.
And other commands wanted to use in the `space` environment can be defined as:

```py
Expand Down
13 changes: 5 additions & 8 deletions examples/example1.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import os
from typing import Union
from kola import KoiLang, BaseLexer, kola_command, kola_text, kola_env
from kola import KoiLang, kola_command, kola_text, kola_env


class MultiFileManager(KoiLang):
def __init__(self) -> None:
super().__init__()
self._file = None

@kola_env
def space(self, name: str) -> None:
path = name.replace('.', '/')
Expand Down Expand Up @@ -41,8 +36,10 @@ def text(self, text: str) -> None:
raise OSError("write texts before the file open")
self._file.write(text)

def parse(self, lexer: Union[BaseLexer, str]) -> None:
super().parse(lexer)
def at_start(self) -> None:
self._file = None

def at_end(self) -> None:
self.end()


Expand Down
4 changes: 2 additions & 2 deletions kola/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def load(self, path: str, type: str = "kola", *, encoding: str = "utf-8") -> Non
cmd_set = _load_script(path, encoding=encoding)
self.push(cmd_set.__name__, cmd_set())
else:
raise KoiLangCommandError("load type only supports 'kola' and 'script'")
raise ValueError("load type only supports 'kola' and 'script'")

@kola_command
def reset(self) -> None:
Expand All @@ -128,7 +128,7 @@ def text(self, text: str) -> None:
break
key = text[j: i]
val = self.get_var(key) or ''
s.write(val)
s.write(str(val))
else:
s.write(text[i])
i += 1
Expand Down
3 changes: 1 addition & 2 deletions kola/klvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __call__(self, *args: Any, **kwds: Any) -> Any:
vmobj: KoiLang = args[0]
env_name, _ = vmobj.top
if env_name not in self.envs:
raise ValueError(f"unmatched environment {env_name}")
raise KoiLangCommandError(f"unmatched environment {env_name}")
return self.__func__(*args, **kwds)

def __repr__(self) -> str:
Expand Down Expand Up @@ -383,7 +383,6 @@ def at_end(self) -> None:
def parse(self, lexer: Union[BaseLexer, str]) -> None:
"""
Parse kola text or lexer from other method.
Simple reload it to add more functions.
"""
if isinstance(lexer, str):
lexer = StringLexer(lexer)
Expand Down
4 changes: 2 additions & 2 deletions kola/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.2.0b1"
version_info = (0, 2, 0, "beta", 1)
__version__ = "0.2.0b2"
version_info = (0, 2, 0, "beta", 2)
6 changes: 1 addition & 5 deletions tests/test_klvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@ def step_4(self) -> str:

@kola_env_class("env2") # type: ignore
class KolaTestEnv(KoiLang):
def __init__(self) -> None:
super().__init__()
self.l_num = []

@kola_text
def enter(self, text: str) -> str:
self.l_num.append(text)
self.l_num = [text]
return text

@kola_command
Expand Down

0 comments on commit 0f8f2e4

Please sign in to comment.