Skip to content

Commit 197695b

Browse files
committed
Document timezone and add Exmaple to markdown generation
Signed-off-by: Lehlogonolo Poole <[email protected]>
1 parent a24c85d commit 197695b

File tree

6 files changed

+3140
-14
lines changed

6 files changed

+3140
-14
lines changed

docs/libs/timezone.md

Lines changed: 3007 additions & 0 deletions
Large diffs are not rendered by default.

src/main/java/com/jaiva/Main.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.jaiva.interpreter.libs.Globals;
1313
import com.jaiva.interpreter.runtime.IConfig;
1414
import com.jaiva.tokenizer.*;
15+
import com.jaiva.tokenizer.tokens.TSymbol;
1516
import com.jaiva.tokenizer.tokens.Token;
1617
import com.jaiva.tokenizer.tokens.specific.TArrayVar;
1718
import com.jaiva.tokenizer.tokens.specific.TDocsComment;
@@ -364,7 +365,7 @@ public static ArrayList<Token<?>> parseTokens(String filePath, boolean returnVfs
364365
ArrayList<Token<?>> tokens = new ArrayList<>();
365366
MultipleLinesOutput m = null;
366367
BlockChain b = null;
367-
Scanner scanner = null;
368+
Scanner scanner;
368369
try {
369370
scanner = new Scanner(myObj);
370371
} catch (FileNotFoundException e) {
@@ -394,8 +395,7 @@ public static ArrayList<Token<?>> parseTokens(String filePath, boolean returnVfs
394395
tks.remove(t);
395396
continue;
396397
}
397-
if (comment == null || (!(l instanceof TArrayVar) && !(l instanceof TUnknownScalar)
398-
&& !(l instanceof TFunction)))
398+
if (comment == null || !(l instanceof TSymbol))
399399
continue;
400400
JDoc doc = new JDoc(lineNum, comment.trim());
401401
l.tooltip = doc;
@@ -404,7 +404,7 @@ public static ArrayList<Token<?>> parseTokens(String filePath, boolean returnVfs
404404
}
405405
}
406406
comment = null;
407-
tokens.addAll((ArrayList<Token<?>>) tks);
407+
tokens.addAll(tks);
408408
}
409409
case BlockChain blockChain -> {
410410
m = null;
@@ -418,12 +418,11 @@ public static ArrayList<Token<?>> parseTokens(String filePath, boolean returnVfs
418418
+ ((TDocsComment) token1.value()).comment;
419419
}
420420
case Token<?> token -> {
421-
TokenDefault t = ((TokenDefault) token.value());
421+
TokenDefault t = token.value();
422422
if (returnVfs && !t.exportSymbol) {
423423
// dont do anythin.
424424
} else if (comment != null
425-
&& ((t instanceof TArrayVar) || (t instanceof TUnknownScalar)
426-
|| (t instanceof TFunction))) {
425+
&& t instanceof TSymbol) {
427426

428427
JDoc doc = new JDoc(lineNum, comment.trim());
429428
t.tooltip = doc;

src/main/java/com/jaiva/ToMarkdown.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ private void run(IConfig<?> i, Path outputDir, boolean globals) throws IOExcepti
7878
} else if (option == 'y') {
7979
ArrayList<ArrayList<TokenDefault>> buckets = Buckets.of(
8080
new ArrayList<>(exportables),
81-
TVariable.class,
82-
TFunction.class);
81+
TFunction.class,
82+
TVariable.class);
8383
printToFile();
8484
for (int j = 0; j < buckets.size(); j++) {
8585
ArrayList<TokenDefault> bucket = buckets.get(j);
8686
printToFile(
8787
new MarkDownLiteral(
88-
(j == 0 ? "Variables" : "Functions")
88+
(j == 0 ? "Functions" : "Variables")
8989
).title(MarkDownLiteral.Title.SUBTITLE).toString()
9090
);
9191
for (TokenDefault t : bucket) {
@@ -208,8 +208,6 @@ private void printSymbolToFile(char option, TokenDefault symbol) throws Tokenize
208208

209209
printToFile();
210210
printToFile(jDoc.getDescription());
211-
// TODO: When example JDoc tag is added, add here.
212-
// printToFile();
213211

214212
if (!jDoc.getParameters().isEmpty()) {
215213
printToFile();
@@ -237,6 +235,17 @@ private void printSymbolToFile(char option, TokenDefault symbol) throws Tokenize
237235
);
238236
}
239237

238+
if (!jDoc.getExample().isEmpty()) {
239+
printToFile();
240+
printToFile(new MarkDownLiteral("Example:").bold().toString());
241+
printToFile(
242+
MarkDownLiteral.asCodeBlock(
243+
jDoc.getExample(),
244+
"jaiva"
245+
)
246+
);
247+
}
248+
240249
if (!jDoc.getNote().isEmpty()) {
241250
printToFile();
242251
printToFile(

src/main/java/com/jaiva/interpreter/Vfs.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import java.util.ArrayList;
88
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.function.BiFunction;
911

1012
/**
1113
* "vfs" stands for Variable FunctionS. It's a class that extends HashMap and in term inherits all it's properties.
@@ -103,4 +105,62 @@ public ArrayList<Token<?>> toTokenList() {
103105
this.forEach((s, mv) -> tokens.add(mv.getValue().token.toToken()));
104106
return tokens;
105107
}
108+
109+
/**
110+
* Sorts the keys of the Vfs in alphabetical order.
111+
*/
112+
public void sortKeys() {
113+
ArrayList<String> keys = new ArrayList<>(this.keySet());
114+
keys.sort(String::compareTo);
115+
HashMap<String, MapValue> sorted = new HashMap<>();
116+
for (String key : keys) {
117+
sorted.put(key, this.get(key));
118+
}
119+
this.clear();
120+
this.putAll(sorted);
121+
}
122+
123+
public ArrayList<MapValue> sortKeys(BiFunction<String, String, Integer> comparator, boolean returnSorted) {
124+
ArrayList<String> keys = new ArrayList<>(this.keySet());
125+
keys.sort(comparator::apply);
126+
HashMap<String, MapValue> sorted = new HashMap<>();
127+
ArrayList<MapValue> sortedValues = new ArrayList<>();
128+
for (String key : keys) {
129+
MapValue mv = this.get(key);
130+
sorted.put(key, mv);
131+
sortedValues.add(mv);
132+
}
133+
this.clear();
134+
this.putAll(sorted);
135+
return returnSorted ? sortedValues : null;
136+
}
137+
138+
/**
139+
* Sorts the keys of the Vfs giving priority to the provided top keys.
140+
* The top keys will appear first in the order they are provided, followed by the rest in alphabetical order.
141+
* @param topKeys The keys to prioritize.
142+
*/
143+
public void sortWithKeyPriority(String ...topKeys) {
144+
ArrayList<String> keys = new ArrayList<>(this.keySet());
145+
ArrayList<String> topKeysList = new ArrayList<>(List.of(topKeys));
146+
keys.sort((a, b) -> {
147+
boolean aIsTop = topKeysList.contains(a);
148+
boolean bIsTop = topKeysList.contains(b);
149+
if (aIsTop && bIsTop) {
150+
return Integer.compare(topKeysList.indexOf(a), topKeysList.indexOf(b));
151+
} else if (aIsTop) {
152+
return -1;
153+
} else if (bIsTop) {
154+
return 1;
155+
} else {
156+
return a.compareTo(b);
157+
}
158+
});
159+
HashMap<String, MapValue> sorted = new HashMap<>();
160+
for (String key : keys) {
161+
sorted.put(key, this.get(key));
162+
}
163+
this.clear();
164+
this.putAll(sorted);
165+
}
106166
}

src/main/java/com/jaiva/interpreter/libs/time/TimeZone.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,20 @@ public TimeZone() {
2929
new ArrayList<>(zoneIds.stream().map(Pair::getSecond).toList()),
3030
-1,
3131
JDoc.builder()
32-
.addDesc("The complete list of timezone variable constants")
32+
.addDesc("The complete list of IANA format timezone constants.")
3333
.sinceVersion("4.2.0")
34+
.addExample("""
35+
tsea "jaiva/timezone"!
36+
37+
@ Get all timezone constants
38+
maak list <- tz_getAll!
39+
@ Print everything with Etc prefix
40+
colonize item with list ->
41+
if (item ? "Etc") ->
42+
khuluma(item)!
43+
<~
44+
<~
45+
""")
3446
.build()
3547
), new ArrayList<>(zoneIds.stream().map(Pair::getSecond).toList()));
3648
getAll.freeze();

src/main/java/com/jaiva/utils/MarkDownLiteral.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public MarkDownLiteral(String input) {
1818
literal.add(input);
1919
}
2020

21+
2122
/**
2223
* Applies bold formatting to the literal.
2324
*
@@ -69,7 +70,7 @@ public MarkDownLiteral blockQuote() {
6970
*/
7071
public MarkDownLiteral linkTo(String URL) {
7172
literal.addFirst("[");
72-
literal.add("](\"" + URL + "\")");
73+
literal.add("](" + URL + ")");
7374
return this;
7475
}
7576

@@ -106,6 +107,44 @@ public MarkDownLiteral comment() {
106107
return this;
107108
}
108109

110+
/**
111+
* Wraps the input list of strings in a Markdown code block with the specified language.
112+
*
113+
* @param input The list of strings to be wrapped.
114+
* @param languageIdentifier The language identifier for the code block.
115+
* @return An ArrayList of strings representing the formatted code block.
116+
*/
117+
public static ArrayList<String> asCodeBlock(ArrayList<String> input, String languageIdentifier) {
118+
ArrayList<String> out = new ArrayList<>();
119+
out.add("```" + languageIdentifier);
120+
out.addAll(input);
121+
out.add("```");
122+
return out;
123+
}
124+
125+
/**
126+
* Converts headers and rows into a Markdown table format.
127+
*
128+
* @param headers The list of header strings.
129+
* @param rows The list of rows, where each row is a list of strings.
130+
* @return An ArrayList of strings representing the formatted Markdown table.
131+
*/
132+
public static ArrayList<String> asTable(ArrayList<String> headers, ArrayList<ArrayList<String>> rows) {
133+
ArrayList<String> out = new ArrayList<>();
134+
// Create header row
135+
String headerRow = "| " + String.join(" | ", headers) + " |";
136+
out.add(headerRow);
137+
// Create separator row
138+
String separatorRow = "| " + String.join(" | ", headers.stream().map(h -> "---").toArray(String[]::new)) + " |";
139+
out.add(separatorRow);
140+
// Create data rows
141+
for (ArrayList<String> row : rows) {
142+
String dataRow = "| " + String.join(" | ", row) + " |";
143+
out.add(dataRow);
144+
}
145+
return out;
146+
}
147+
109148
/**
110149
* Creates a GitHub-style blockquote with a specific alert type.
111150
*

0 commit comments

Comments
 (0)