Skip to content

Commit

Permalink
Fluent parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantinos Bairaktaris committed Oct 20, 2022
1 parent a6b056f commit 05b11f8
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
52 changes: 52 additions & 0 deletions openformats/formats/fluent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import itertools
import re

from fluent.syntax import ast, parse
from openformats.strings import OpenString
from openformats.transcribers import Transcriber
from openformats.utils.compilers import OrderedCompilerMixin

from ..handlers import Handler


class FluentHandler(OrderedCompilerMixin, Handler):
name = "FLUENT"
extension = "ftl"
EXTRACTS_RAW = True

def parse(self, source, is_source=False):
transcriber = Transcriber(source)
source = transcriber.source
parsed = parse(source)
stringset = []
order = itertools.count()
for item in parsed.body:
if not isinstance(item, (ast.Message, ast.Term)):
continue
key = item.id.name
value = source[item.value.span.start : item.value.span.end]
string = OpenString(key, value, order=next(order))
stringset.append(string)
transcriber.copy_until(item.value.span.start)
transcriber.add(string.template_replacement)
transcriber.skip(item.value.span.end - item.value.span.start)
transcriber.copy_to_end()
return transcriber.get_destination(), stringset

@staticmethod
def unescape(string: str):
lines = string.splitlines()
if len(lines) > 1:
indent = re.search("^s*", lines[1]).end()
result = [lines[0].strip()]
for line in lines[1:]:
if not line[:indent].isspace():
return string
result.append(line[indent:].strip())
return " ".join(lines)
else:
return string

@staticmethod
def escape(string):
return string
Empty file.
21 changes: 21 additions & 0 deletions openformats/tests/formats/fluent/files/1_en.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## Closing tabs

tabs-close-button = Close
tabs-close-tooltip = {$tabCount ->
[one] Close {$tabCount} tab
*[other] Close {$tabCount} tabs
}
tabs-close-warning =
You are about to close {$tabCount} tabs.
Are you sure you want to continue?
## Syncing

-sync-brand-name = Firefox Account
sync-dialog-title = {-sync-brand-name}
sync-headline-title =
{-sync-brand-name}: The best way to bring
your data always with you
sync-signedout-title =
Connect with your {-sync-brand-name}
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pyparsing==2.2.0
six
lxml==4.6.2
beautifulsoup4==4.9.3
fluent.syntax==0.18.1

# InDesign
git+https://github.com/kbairak/ucflib@py3_compatibility

0 comments on commit 05b11f8

Please sign in to comment.