Browse Source

Implement support for whitespace

Thomas Dy 7 years ago
parent
commit
7686e55168
2 changed files with 12 additions and 6 deletions
  1. 1 0
      dist/style.css
  2. 11 6
      src/kana.ts

+ 1 - 0
dist/style.css

@@ -1,6 +1,7 @@
 .kana {
   display: inline-block;
   position: relative;
+  white-space: pre;
 }
 
 .kana::after {

+ 11 - 6
src/kana.ts

@@ -113,6 +113,12 @@ namespace kana {
     [index: string]: StateMachine
   }
 
+  const WHITESPACE = state.buildFromTransitions('_', [
+    t('_', '_', ''),
+    t('_', ' ', '')
+  ]);
+
+
   const SINGLE_KANA_MAPPING: KanaMapping = {
     "あ": literal('a'),
     "い": literal('i'),
@@ -241,11 +247,6 @@ namespace kana {
       let remaining = input.toLowerCase() + ' ';
       while (remaining.length > 1) {
         let nextOne = remaining.substring(0, 1);
-        if (/\s/.test(nextOne)) {
-          remaining = remaining.substring(1);
-          continue;
-        }
-
         let nextTwo = remaining.substring(0, 2);
         let doubleKana = DOUBLE_KANA_MAPPING[nextTwo];
         if (doubleKana != undefined) {
@@ -274,7 +275,11 @@ namespace kana {
               }
             } else {
               kana.push(nextOne);
-              machines.push(literal(nextOne));
+              if (/\s/.test(nextOne)) {
+                machines.push(WHITESPACE.clone());
+              } else {
+                machines.push(literal(nextOne));
+              }
             }
 
             prevTsu = false;