public async Task <CambridgeWordInfo> GetWordInfo(string word) { var urlEncodedWord = WebUtility.UrlEncode(word); var response = await _client.GetStringAsync($"/us/dictionary/english/{urlEncodedWord}"); if (response.Contains("Popular searches")) { response = await _client.GetStringAsync($"/search/english/direct/?q={urlEncodedWord}"); } var parser = new HtmlParser(); var document = parser.Parse(response); var dataSetSelector = GetSelectorFromDataSet(DataSet); var dataSetElement = document.QuerySelector(dataSetSelector); if (dataSetElement == null) { return(null); } var wordInfo = new CambridgeWordInfo(); wordInfo.InputWord = word; foreach (var entryElement in dataSetElement.QuerySelectorAll(".entry-body > div")) { var parsedEntry = ParseEntryFromEntryElement(entryElement); if (parsedEntry != null) { wordInfo.Entries.Add(ParseEntryFromEntryElement(entryElement)); } } return(wordInfo); }
public Task <bool> AddNote(string deckName, string modelName, CambridgeWordInfo wordInfo, IWordInfoFormatter formatter, bool checkIfExisting = false) { var front = wordInfo.InputWord; var back = wordInfo.AsFormatted(formatter); return(AddNote(deckName, modelName, front, back, checkIfExisting)); }
public void RemoveItemByWordInfo(CambridgeWordInfo wordInfo, bool monitorChangesMade) { bool localChangesMade = false; foreach (WordViewItem wordViewItem in lvWords.Items) { if (wordViewItem.WordInfo != wordInfo) { continue; } lvWords.Items.Remove(wordViewItem); localChangesMade = true; if (monitorChangesMade) { _changeMade = true; } } if (!localChangesMade) { return; } RefreshWordColumn(); rtbWordOutput.Text = string.Empty; }
public ExampleViewItem(CambridgeWordInfo wordInfo, int entryIndex, int definitionIndex, int exampleIndex = -1) { EntryIndex = entryIndex; DefinitionIndex = definitionIndex; ExampleIndex = exampleIndex; WordInfo = wordInfo; }
public WordViewItem(string word) { var wordInfo = new CambridgeWordInfo(); wordInfo.InputWord = word; WordInfo = wordInfo; if (string.IsNullOrWhiteSpace(word)) { SubItems[2].Text = "Adding"; } }
public EditWordInfoForm(DeckManagementForm sender, Job job, CambridgeProvider cambridgeProvider, CambridgeWordInfo wordInfo, ref List <CambridgeWordInfo> wordInfos) { _sender = sender; Job = job; _cambridgeProvider = cambridgeProvider; WordInfo = wordInfo; _wordInfos = wordInfos; InitializeComponent(); Initialize(); _titleFormat = Text; }
public string Render(CambridgeWordInfo wordInfo) { if (wordInfo.Entries.Count == 0) { return(string.Empty); } var sb = new StringBuilder(); for (var i = 0; i < wordInfo.Entries.Count; i++) { var entry = wordInfo.Entries[i]; if (i == 0 || wordInfo.Entries.Count > 1 && wordInfo.Entries[i - 1].ActualWord != entry.ActualWord) { sb.AppendLine(entry.ActualWord); } if (!string.IsNullOrWhiteSpace(entry.Label)) { sb.AppendLine($"[{entry.Label}]"); } foreach (var definition in entry.Definitions) { sb.AppendLine(definition.Definition); if (definition.Examples == null) { continue; } foreach (var example in definition.Examples) { if (!string.IsNullOrWhiteSpace(example)) { sb.AppendLine($" -> \"{example}\""); } } } sb.AppendLine(); } if (sb.Length == 0) { return(string.Empty); } var sbText = sb.ToString(); return(sbText.Substring(0, sbText.Length - 1)); }
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (string.IsNullOrWhiteSpace(tbDeckName.Text)) { MessageBox.Show("Deck name cannot be empty."); return; } var previousDeckName = Deck.Name; Deck.Name = tbDeckName.Text; if (previousDeckName != Deck.Name) { _changeMade = true; } var previousDeckExportOption = Deck.ExportOption; Deck.ExportOption = rbText.Checked ? "Text" : "HTML"; if (previousDeckExportOption != Deck.ExportOption) { _changeMade = true; } if (!_changeMade) { DialogResult = DialogResult.Cancel; return; } Deck.DateModified = DateTime.Now; var wordInfos = lvWords.Items.Cast <WordViewItem>() .Select(wordViewItem => wordViewItem.WordInfo) .Where(wordInfo => wordInfo != null) .OrderBy(a => a.InputWord, _comparer).ToArray(); if (previousDeckName == Path.GetFileNameWithoutExtension(Deck.FilePath)) { File.Delete(Deck.FilePath); Deck.FilePath = Deck.GetDeckFilePathFromDeckName(Deck.Name); File.WriteAllBytes(Deck.FilePath, CambridgeWordInfo.Serialize(wordInfos)); } else { File.WriteAllBytes(Deck.FilePath, CambridgeWordInfo.Serialize(wordInfos)); } File.WriteAllBytes(Deck.FilePath, CambridgeWordInfo.Serialize(wordInfos)); DialogResult = DialogResult.OK; }
private async Task LookUpWord(WordViewItem wordViewItem = null, string word = null) { word = wordViewItem.Text.Trim().ToLower(); CambridgeWordInfo wordInfo = null; try { wordInfo = await _cambridgeProvider.GetWordInfo(word); if (wordInfo == null) { Invoke(new Action(() => wordViewItem.SubItems[2].Text = "Cannot find word.")); Debug.WriteLine($"Error:: Word ({word}): Cannot find word."); return; } Invoke(new Action(() => wordViewItem.WordInfo = wordInfo)); _changeMade = true; await Task.Delay(1000); } catch (Exception exception) { Debug.WriteLine($"Internal Error:: Word ({word}): {exception.Message}"); } }
private void LoadDataFile(string fileName) { if (!File.Exists(fileName)) { return; } var readData = File.ReadAllBytes(fileName); var deserialized = CambridgeWordInfo.Deserialize(readData); if (deserialized.Length == 0) { return; } var listViewItems = new List <WordViewItem>(); foreach (var wordInfo in deserialized) { listViewItems.Add(new WordViewItem(wordInfo)); } lvWords.Items.AddRange(listViewItems.ToArray()); RefreshWordColumn(); }
public WordViewItem(CambridgeWordInfo wordInfo) { WordInfo = wordInfo; }
public string Render(CambridgeWordInfo wordInfo) { CambridgeWordInfo.Entry previousEntry = null; var blocksHtml = Resources.BlocksFormat; var entryBuilder = new StringBuilder(); for (int index = 0; index < wordInfo.Entries.Count; index++) { var actualWordDoesntEqualInput = !string.Equals(wordInfo.Entries[index].ActualWord, wordInfo.InputWord, StringComparison.CurrentCultureIgnoreCase); var currentActualWordIsPreviousActualWord = previousEntry != null && previousEntry.ActualWord != wordInfo.Entries[index].ActualWord; var entryHtml = Resources.EntryFormat; if (index == 0 && wordInfo.InputWord == wordInfo.Entries[0].ActualWord) { entryHtml = entryHtml.Replace("{{ActualWord}}", string.Empty); } else if (!actualWordDoesntEqualInput && !currentActualWordIsPreviousActualWord) { entryHtml = entryHtml.Replace("{{ActualWord}}", string.Empty); } else { entryHtml = entryHtml.Replace("{{ActualWord}}", $"<div class=\"word\">{wordInfo.Entries[index].ActualWord}</div>"); } if (string.IsNullOrWhiteSpace(wordInfo.Entries[index].Label)) { entryHtml = entryHtml.Replace("<p class=\"label\">[{{Label}}]</p>", string.Empty); } else { entryHtml = entryHtml.Replace("{{Label}}", wordInfo.Entries[index].Label); } var scopeBuilder = new StringBuilder(); foreach (var definition in wordInfo.Entries[index].Definitions) { var scopeFormat = Resources.ScopeFormat.Replace("{{Definition}}", definition.Definition); if (definition.Examples == null) { scopeFormat = scopeFormat.Replace("<div class=\"examples\">{{Examples}}</div>", string.Empty); } else { var examplesBuilder = new StringBuilder(); foreach (var example in definition.Examples) { if (string.IsNullOrWhiteSpace(example)) { continue; } int offset = 0; if (wordInfo.Entries[index].ActualWord.StartsWith("-")) { offset = 1; } var pattern = $"\\b\\w*{wordInfo.Entries[index].ActualWord.Substring(offset, wordInfo.Entries[index].ActualWord.Length - (2 + offset))}\\w*\\b"; var replacement = "<span class=\"highlighted\">$&</span>"; var formattedExample = Regex.Replace(example, pattern, replacement, RegexOptions.IgnoreCase); examplesBuilder.AppendLine(Resources.ExampleFormat.Replace("{{Example}}", formattedExample)); } scopeFormat = scopeFormat.Replace("{{Examples}}", examplesBuilder.ToString()); } scopeBuilder.Append(scopeFormat); } entryHtml = entryHtml.Replace("{{Scope}}", scopeBuilder.ToString()); entryBuilder.AppendLine(entryHtml); previousEntry = wordInfo.Entries[index]; } return(HtmlMinifier.Minify(blocksHtml.Replace("{{Blocks}}", entryBuilder.ToString()))); }
public DefinitionViewItem(CambridgeWordInfo wordInfo, int entryIndex, int definitionIndex) { EntryIndex = entryIndex; DefinitionIndex = definitionIndex; WordInfo = wordInfo; }